天天看点

Git 配置文件

配置自己的Git工作环境。配置工作只需要一次,以后升级可以沿用现在的配置。当然,我们也可以随时使用相同的命令修改已有的配置。

Git提供了一个叫做git config【译注:实际是 

git-config

 命令,只不过可以通过 

git

 加一个名字来呼叫此命令】的工具,专门用来读取或者配置Git工作的环境变量。而正是由这些环境变量决定了在各个环节的具体工作方式和行为。这些变量可以存放在以下三个不同的地方:

  1. $INSTALL_PATH/etc/gitconfig 文件:对整个系统中所有用户都适用的配置。

    [$INSTALL_PATH为Git的安装目录]

    git config --system ... # 此命令读写$INSTALL_PATH/etc/gitconfig 文件

  2. ~/.gitconfig 文件:当前用户的配置文件,只适用当前用户。

    git config --global ... # 此命令读写 ~/.gitconfig 文件

  3. 当前项目的配置文件(即工作目录中的.git/config文件):此配置文件只适用当前项目。

每一个级别的配置都会覆盖上层的相同配置,即.git/config里的配置会覆盖~/.gitconfig里的相同配置,~/.gitconfig里的配置会覆盖 $INSTALL_PATH/etc/gitconfig里的相同配置【以权限来理解即.git/config > ~/.gitconfig > $INSTALL_PATH/etc/gitconfig】。

#########################################################################################

    windows系统中,Git会寻找用户主目录下的 .gitconfig 文件。此外,Git还会尝试寻找

    $INSTALL_PATH/etc/gitconfig文件。

设定配置文件:

目前没有项目,所以先只配置$INSTALL_PATH/etc/gitconfig和~/.gitconfig配置文件比较。

配置$INSTALL_PATH/etc/gitconfig文件:[我的git安装目录为/opt/git]

[root@git ~]# git config --system user.name "Milo Zhao"
error: could not lock config file /opt/git//etc/gitconfig: ?????????
报错,默认在Git安装目录下是没有etc目录的:
[root@git ~]# ll /opt/git/
总用量 16
drwxr-xr-x. 2 root root 4096 10月 16 02:08 bin
drwxr-xr-x. 3 root root 4096 10月 16 02:08 lib64
drwxr-xr-x. 3 root root 4096 10月 16 02:08 libexec
drwxr-xr-x. 9 root root 4096 10月 16 02:08 share
[root@git ~]# mkdir /opt/git/etc
[root@git ~]# git config --system user.name "Milo Zhao"
[root@git ~]# git config --system user.email [email protected]
[root@git ~]# ll /opt/git/etc/gitconfig 
-rw-r--r--. 1 root root 25 10月 16 19:10 /opt/git/etc/gitconfig
[root@git ~]# less /opt/git/etc/gitconfig
[user]
        name = Milo Zhao
        email = [email protected]
/opt/git/etc/gitconfig (END)      

配置~/.gitconfig文件:

[root@git ~]# git config --global user.name "Nine Zhao"
[root@git ~]# git config --global user.email [email protected]
[root@git ~]# whoami
root
[root@git ~]# pwd
/root
[root@git ~]# cat .gitconfig 
[user]
        name = Nine Zhao
        email = [email protected]      

查看配置信息

[root@git ~]# git config --list
user.name=Milo Zhao
[email protected]
user.name=Nine Zhao
[email protected]
...      

我们看到有重复的变量名[user.name和user.email],这是因为它们来自不同的配置文件,上面的重复变量名分别来自$INSTALL_PATH/etc/gitconfig和~/.gitconfig,但是最终Git实际采用的是最后一个。

我们也可以直接查阅某个环境变量的设定,只要把变量名跟在后面即可:

[root@git ~]# git config user.name
Nine Zhao
[root@git ~]# git config user.email
[email protected]      

显示的均是重复变量名的最后一个。

其他项设置:

  1. 文本编辑器:Git需要输入一些额外消息的时候,会自动调用一个外部文本编辑器。我们可以设置此默认文本编辑器为我们自己熟悉的文本编辑器。一般默认可能是vi或vim。 
  2. [root@git ~]# git config --global core.editor vim
    [root@git ~]# git config core.editor
    vim      
  3. [root@git ~]# git config --global merge.tool vimdiff
    [root@git ~]# git config merge.tool
    vimdiff