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]
      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