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 harrison.f@gmail.com
使用了 --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