一个计算机技术爱好者与学习者

0%

好好学Git:Git安装与配置

1. Git简介

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git是一个免费的开源分布式版本控制系统,旨在快速高效地管理项目的所有文件。Git中的项目被称为仓库(repository),或者是代码库。

本文主要学习Git的安装和配置,是我们使用Git的第一步。
参考文档:

2. 安装Git

2.1. Windows

在Windows操作系统中,推荐使用Git官方提供的Git for Windows
访问Git官方下载页面,点击Windows图标下载exe文件,然后按照提示进行安装即可。

2.2. macOS

在macOS操作系统中,推荐使用Homebrew进行安装。在终端中输入以下命令:

1
brew install git

Homebrew的安装方法,可以参考《MacOS安装配置Homebrew》

2.3. Linux

在Linux操作系统中,可以使用包管理器进行安装。在Ubuntu/Debian系统中,使用如下命令:

1
apt-get install git

在CentOS系统中,使用如下命令:

1
yum install git

3. 配置Git用户信息

3.1. 全局配置

1
2
3
git config --global --list
git config --global user.name "voidking"
git config --global user.email "voidking@qq.com"

3.2. 针对单独项目配置

1
2
3
git config --list
git config user.name "haojin"
git config user.email "voidking@vip.qq.com"

提交的时候,如果项目自身没有配置信息就会使用全局配置,有配置就会使用单独的配置信息。

4. 配置中文不转义

git status看到的中文文件名,不要转义

1
git config --global core.quotepath false

5. 配置免密上传下载

5.1. 配置免密上传下载概述

配置免密上传下载,有两种方式:一种是使用ssh密钥对,一种是使用access token。
使用ssh密钥对时,需要通过ssh协议上传下载代码;使用access token时,需要通过https协议上传下载代码。

5.2. 使用ssh密钥对

1、生成ssh密钥对
参考《shell命令之ssh》,生成ssh密钥对。

2、在git remote server上添加ssh公钥
打开公钥(id_rsa.pub),复制全文。访问git remote server的ssh配置页面,把公钥粘贴进去。
例如,如果git remote server 是 github,那么访问 settings-ssh 页面,New SSH key,把公钥粘贴进去即可。

3、测试

1
ssh git@github.com

可能出现下面的提示,表明配置成功了:

1
2
3
4
5
6
The authenticity of host 'github.com (192.30.252.128)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.
Hi voidking! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

5.3. 指定ssh私钥

5.3.1. 方法一:使用ssh配置

修改 ~/.ssh/config 文件,添加:

1
2
3
4
host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
User git

5.3.2. 方法二:使用环境变量

1
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_github -F /dev/null" git clone https://github.com/voidking/wecms.git

5.4. 使用 access token

1、生成access token
访问github的 Personal access tokens 页面,Generate new token。
Note输入 voidking-pc ,Select scope选择repo,然后点击Generate token。生成了一个token,保存它。

2、开启密码记录

1
git config --global credential.helper store

用户家目录下的 .gitconfig 文件末尾会添加:

1
2
[credential]
helper = store

3、记住密码
再次使用git pull或者git push,系统会提示输入git用户名和密码,这里的密码输入access token即可,不要使用真实密码。
系统会记住用户的access token,存储在用户家目录下的 .git-credentials 文件中。之后就不再需要输入git密码了。

5.5. 修改 access token

如果access token过期需要替换,那么需要执行以下步骤:
1、清除当前token

1
2
3
git credential reject
protocol=https
host=github.com

2、使用新token
再次使用git pull或者git push,系统会提示输入git用户名和密码,这里的密码输入access token即可。

或者,使用命令设置新token(不建议,macos上经尝试无效)。

1
2
3
git credential fill
protocol=https
host=github.com

这时 Git 会提示输入新的密码。输入后保存即可。

6. 多个用户配置免密上传下载

如果我们有多个 github 账户或者多个 gitlab 账户,都想要免密上传和下载代码,该怎样配置?

如果是使用ssh密钥对的方式,那么比较简单,只要给这些不同账户配置相同的公钥即可。

如果是使用access token的方式,那么就麻烦一些了。
已知 ~/.git-credentials 文件中记录了 github.com 的账密,所有和 github.com 的交互都会使用这个账密。
而我们有多个用户名,不同的仓库需要使用不同的用户名进行操作,那么 .git-credentials 文件中会记录多个 github.com 的账密,例如:

1
2
https://voidking:ghp_xxx@github.com
https://quizthink:ghp_xxx@github.com

问题来了,在实际上传下载代码时,项目怎么知道该使用哪一条账密记录呢?
解决办法:在项目的配置中指定用户名

1
vim .git/config

添加配置:

1
2
[credential]
username = quizthink

如果是首次使用一个新的用户,那么在执行git pull或者git push时,系统会提示输入这个新用户的密码,然后保存到 .git-credentials 文件中。

注意:如果在仓库中不配置 username ,那么默认会使用 .git-credentials 文件中的最后一个 github.com 账密。

7. 配置使用代理

如果克隆项目特别慢,自己又有科学上网的ssr,那么可以开启代理。

1
2
git config --global http.proxy 'socks5://127.0.0.1:1080' 
git config --global https.proxy 'socks5://127.0.0.1:1080'

关闭代理:

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy