Git(1)- 使用前的基本配置

为了更好的使用体验 Git,在正式使用 Git 前需要进行一些必要的设置。

设置用户信息

安装完 Git 之后,要做的第一件事就是设置你的用户名和邮件地址。每一个 Git 提交都会使用这些信息,它们会写入到你的每一次提交中,不可更改:

1
2
3
4
5
6
# git config --global user.name "yourname"
# git config --global user.email "youremail"

# 示例
git config --global user.name "ct"
git config --global user.email "ct@163.com"

设置行尾首选项

每次按键盘上的 return 时,会插入一个称为行结束符的不可见字符。 不同的操作系统处理行结束符的方式不同。

在使用 Git 和 GitHub 协作处理项目时,Git 可能产生意外结果,例如,您在 Windows 计算机上操作,而您的协作者是在 macOS 中做的更改。

您可以将 Git 配置为自动处理行结束符,以便与使用不同操作系统的人员有效地协作。

1
2
git config --global core.autocrlf input
git config --global core.safecrlf true
1
2
git config --global core.autocrlf true
git config --global core.safecrlf true

设置默认分支名

分支名默认为 Master ,但是 Github 仓库默认分支名修改为了 main ,由于需要经常使用 Github ,因此修改默认分支名为 main。如果你主要是有 Gitee ,则可以不修改。

1
2
# git config --global init.defaultBranch "分支名"
git config --global init.defaultBranch "main" # 为配合 github 使用

设置文本编辑器

可以配置一个你熟悉使用的编辑器作为 Git 的默认编辑器,通过下面的指令配置。

1
2
# git config --global core.editor "编辑器名"
git config --global core.editor vim # 终端个人比较习惯 vim 的操作

检查配置信息

如果想要检查你的配置,可以使用 git config —list 命令来列出所有 Git 当时能找到的配置。

1
2
3
4
5
6
7
8
$ git config --list
user.name=John Doe
user.email=johndoe@example.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...

当然你也可以在用户目录下的 .gitconfig 文件中查看所有配置。