Git Config

git config有三个级别: systemglobal (用户, 全局生效) 和local (用户, 当前仓库) , 优先级为system>global>local, 分别使用--system/global/local参数来指定级别。

  • 查看系统 (system) 的git config:

    1
    
    git config --system --list
    
  • 查看用户全局 (global) 的git config:

    1
    
    git config --global --list
    
  • 查看当前仓库 (local) 的git config:

    1
    
    git config --local --list
    
不使用参数时
如果不使用--system/global/local参数, 直接使用git config --list命令, 则输出system+global+local的git config; 如果当前路径不是git仓库, 则只输出system+global的git config。
1
2
git config --global user.name "your_name"
git config --global user.email "your_email@example.com"
1
ssh-keygen -t ed25519 -C "your_email@example.com"

如果你使用的是不支持Ed25519算法的旧系统, 请使用以下命令:

1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
注意

这里的xxxxx@xxxxx.com只是生成的sshkey的名称, 并不约束或要求具体命名为某个邮箱。

现网的大部分教程均讲解的使用邮箱生成, 其一开始的初衷仅仅是为了便于辨识所以使用了邮箱。

在Git仓库托管处 (GitHub、GitLab或Gitee等) 设置好刚才生成的公钥, 测试连通性:

1
2
3
4
5
ssh -T git@github.com
or
ssh -T git@gitlab.example.com
or
ssh -T git@gitee.com

行尾结束符, 即EOL(End of Line)。

Linux和MacOS使用LF的系统, 设置为input:

1
git config --global core.autocrlf input

Windows这个使用CRLF的, 设置为true:

1
git config --global core.autocrlf true

输入错误命令时发出提示, 并等待3秒:

1
git config --global help.autocorrect 30

由于有些学校、企业、机场会屏蔽SSH默认端口号22, 可以修改~/.ssh/config文件以指定端口号, 比如改用443端口:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Github
Host github.com
    HostName ssh.github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519
    Port 443
    User git

# Gitlab
Host gitlab.com
    HostName altssh.gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519
    Port 443
    User git

# Gitee
Host gitee.com
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519
    Port 22
    User git