1. 程式人生 > >git config 介紹

git config 介紹

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的配置檔案還沒有生成。當你配置了一個資訊後,相應的檔案就會自動生成。

[email protected]:~$ git config --global --list
fatal: unable to read config file '/home/harrison/.gitconfig': No such file or directory
[email protected]
:~$ 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