git config 介绍

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuxiao723846/article/details/83113317

Git的三个重要配置文件分别是/etc/gitconfig${HOME}/.gitconfig,.git/config。这三个配置文件都是Git运行时所需要读取的,但是它们分别作用于不同的范围。

  • /etc/gitconfig: 系统范围内的配置文件,适用于系统所有的用户; 使用 git config 时, 加 --system 选项,Git将读写这个文件。
  • ${HOME}/.gitconfig: 用户级的配置文件,只适用于当前用户; 使用 git config 时, 加 --global 选项,Git将读写这个文件。
  • .git/config: Git项目级的配置文件,位于当前Git工作目录下,只适用于当前Git项目; 使用 git config 时,不加选项( --system 和 --global  ),Git将读写这个文件。

每个级别的配置都会覆盖( override )上层的相同配置,覆盖的顺序是 .git/config  --> ${HOME}/.gitconfig --> /etc/gitconfig , 可越级覆盖。比如 .git/config 里的配置会覆盖 /etc/gitconfig 中的同名变量。在 Windows 系统上,Git 会寻找用户主目录下的 .gitconfig 文件。主目录即 ${HOME} 变量指定的目录,一般都是C:\Documents and Settings\${USER}。另外,你也可以使用 --file 或者 -f 来指定想要读写的配置文件。比如:

$ git config --file .git/config user.name  # 查阅项目配置信息里的用户信息。
$ git config --file .git/config user.name "Harrison F" # 将用户信息配置到项目的配置文件中。

了解了这三个文件后,下面我们来看看git config的使用。当你在第一次安装完Git后,直接运行 git config --system --list, git config --global --list, 你将会分别看到下面的错误信息。没关系,这是因为你还没有配置过Git,Git的配置文件还没有生成。当你配置了一个信息后,相应的文件就会自动生成。

harrison@ubuntu:~$ git config --global --list
fatal: unable to read config file '/home/harrison/.gitconfig': No such file or directory
harrison@ubuntu:~$ git config --system --list
fatal: unable to read config file '/etc/gitconfig': No such file or directory

1、接下来我们看一些常用配置:

1)用户信息(user.*)
       首先,需要配置的是你的用户名和Email地址。这两条配置非常重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,所以会随更新内容一起被永久纳入历史记录。

$ git config --global user.name "Harrison F"
$ git config --global user.email [email protected]

使用了 --global 选项,所以,这些信息将被写入 ${HOME}/.gitconfig 中; 如果在特定的项目中,要使用其他的用户名或者Email地址,只需重新配置时去掉 --global选项,新的信息将被写入 .git/config 中。 设置完这两条基本信息后,你应该可以提交更新了,但是为了让我们更方便的使用Git,我们还有几个重要的信息需要配置。
2)文本编辑器(core.editor)
Git会在 需要你输入一些额外消息的时候自动调用一个外部文本编辑器给你使用。 默认会使用操作系统指定的默认编辑器,一般可能会是 Vi 或者 Vim。如果你有其他偏好,比如 Gedit的话,可以重新设置:
$ git config --global core.editor gedit
3)字体颜色(color.*)
如果这个信息不配置,那么你与Git交互时,所有字体的颜色都将是默认系统的一个颜色,很难看,而也不方便我们看出更新和变化。

$ git config --global color.ui auto
$ git config --global color.status auto
$ git config --global color.branch auto
$ git config --global color.diff auto
$ git config --global color.interactive auto

4)差异分析工具(merge.tool)
差异分析工具是用来解决合并冲突的,Git将在出现合并冲突时自动调用配置好的差异分析工具。
$ git config --global merge.tool vimdiff                # 使用Vimdiff作为差异分析工具
5)配置代理(http.proxy)
一般在公司内想要获取互联网上的Git项目,都要求设置HTTP代理。这里将设置最简单的HTTP代理。
$ git config --global http.proxy http://proxy.companyname.com:8080/ 

2、基本命令:

我们这里学习最基本的Git配置和git config命令基本的用法。那么我们如果想删除一些不想要的配置怎么办呢?最直接办法就是编辑配置文件,但是这里还有更简单的命令来删除不想要的配置信息。

1)增:

git config --global --add configName configValue

2)删:

git config  --global --unset configName   (只针对存在唯一值的情况)

3)改:

git config --global configName configValue

4)查:

git config --global configName

示例:

//查
git config --global --list
git config --global user.name
 
//增
git config  --global --add user.name jianan
 
//删
git config  --global --unset user.name
 
//改
git config --global user.name jianan

猜你喜欢

转载自blog.csdn.net/liuxiao723846/article/details/83113317
今日推荐