Git 配置#

概述#

您的个人 git 配置保存在您主目录中的 .gitconfig 文件中。 这是一个 .gitconfig 文件的示例

[user]
        name = Your Name
        email = you@yourdomain.example.com

[alias]
        ci = commit -a
        co = checkout
        st = status -a
        stat = status -a
        br = branch
        wdiff = diff --color-words

[core]
        editor = vim

[merge]
        summary = true

您可以直接编辑此文件,也可以使用 git config --global 命令

git config --global user.name "Your Name"
git config --global user.email you@yourdomain.example.com
git config --global alias.ci "commit -a"
git config --global alias.co checkout
git config --global alias.st "status -a"
git config --global alias.stat "status -a"
git config --global alias.br branch
git config --global alias.wdiff "diff --color-words"
git config --global core.editor vim
git config --global merge.summary true

要在另一台计算机上进行设置,您可以复制您的 ~/.gitconfig 文件,或者运行上面的命令。

详细信息#

user.name 和 user.email#

最好告诉 git 您的身份,以便标记您对代码所做的任何更改。 最简单的方法是从命令行执行

git config --global user.name "Your Name"
git config --global user.email you@yourdomain.example.com

这会将设置写入您的 git 配置文件,该文件现在应该包含一个带有您的姓名和电子邮件的 user 部分

[user]
      name = Your Name
      email = you@yourdomain.example.com

当然,您需要将 Your Nameyou@yourdomain.example.com 替换为您实际的姓名和电子邮件地址。

别名#

您可能会从一些常用命令的别名中受益。

例如,您可能希望能够将 git checkout 缩短为 git co。 或者您可能希望将 git diff --color-words (它给出了差异的格式良好的输出)别名为 git wdiff

以下 git config --global 命令

git config --global alias.ci "commit -a"
git config --global alias.co checkout
git config --global alias.st "status -a"
git config --global alias.stat "status -a"
git config --global alias.br branch
git config --global alias.wdiff "diff --color-words"

将在您的 .gitconfig 文件中创建一个 alias 部分,其内容如下

[alias]
        ci = commit -a
        co = checkout
        st = status -a
        stat = status -a
        br = branch
        wdiff = diff --color-words

编辑器#

您可能还想确保使用您选择的编辑器

git config --global core.editor vim

合并#

要在执行合并时强制执行摘要(再次是 ~/.gitconfig 文件)

[merge]
   log = true

或者从命令行

git config --global merge.log true