安装和设置
安装
配置文件
配置Git的时候,加上--global是针对当前用户起作用的,如果不加,那只针对当前的仓库起作用。
配置文件放哪了?每个仓库的Git配置文件都放在.git/config文件中:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = [email protected]:michaelliao/learngit.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[alias]
last = log -1
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
别名就在[alias]后面,要删除别名,直接把对应的行删掉即可。
$ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
而当前用户的Git配置文件放在用户主目录下的一个隐藏文件.gitconfig中:
[alias]
co = checkout
ci = commit
br = branch
st = status
[user]
name = Your Name
email = [email protected]
配置别名也可以直接修改这个文件,如果改错了,可以删掉文件重新通过命令配置。
基本设置
在打开Git之前,你需要完成一些最基本的设置。例如你的用户名,你的邮箱地址以及在命令行界面中的一些重要的显示设置:
$ git config --global user.name "wangenius"
$ git config --global user.email "[email protected]"
因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址。你也许会担心,如果有人故意冒充别人怎么办?这个不必担心,首先我们相信大家都是善良无知的群众,其次,真的有冒充的也是有办法可查的。
远程连接
两种远程连接方式
$ git remote -v # (https|http):// 开头的是http方式;git@ 开头的是ssh方式;
http/https连接设置代理
查看当前代理
$ git config --global http.proxy
移除代理配置
$ git config --global --unset http.proxy
$ git config --global --unset http.https://github.com.proxy
命令行模式下配置(可走http代理也可以走socks5代理)
$ git config --global https.proxy https://proxyuser:proxypassword@ip/域名:port
$ git config --global http.proxy http://proxyuser:proxypassword@ip/域名:port
$ git config --global http.proxy socks5://127.0.0.1:10808 #这里的 socks5 仅仅是代理使用的协议,它依然是针对 http 设置的,所以仅对 http 协议的仓库有效。
只对github.com
$ git config --global http.https://github.com.proxy socks5://127.0.0.1:10808
$ git config --global https.https://github.com.proxy https://127.0.0.1:10808
ssh连接设置代理
- 方式一
ssh配置文件地址为:
~/.ssh/config;windows中就是:C:\Users\你的用户名\.ssh\config(若不存在自行创建)
Host github.com *.github.com gitee.com #可针对域名配置不同的代理;亦可以不配置
User git
Port 22 # SSH默认端口22, HTTPS默认端口443
Hostname %h
IdentityFile ~\.ssh\id_rsa # 这里放你的SSH私钥
ProxyCommand connect -S 127.0.0.1:10808 %h %p
- 方式二
sudo apt install corkscrew
IdentityFile ~/.ssh/id_rsa
Host github.com gitee.com
User git
Hostname %h
Port 443
ProxyCommand /usr/bin/corkscrew proxy_server proxy_port %h %p ~/.ssh/proxyauth #proxy_server 为代理服务器地址,proxy_port 为代理服务器端口。
username:password
自定义
比如,让Git显示颜色,会让命令输出看起来更醒目:
$ git config --global color.ui true