【Git】了解 git config 設定

【Git】了解 git config 設定

本篇重點

  • 了解設定區域:--system, --global, --local
  • git config 常見設定
  • 查詢 git config 設定
  • 刪除 git config 設定

設定的區域

依需求做不同範圍的設定,在不同區域的設定影響 git 操作的範圍

當前儲存庫設定 --local

在資料夾建立 git 環境後,這個資料夾就是當前儲存庫的設定區域

設定檔文件通常位於當前資料夾的 .git/config

用戶級別設定 --global

系統用戶的設定區域,會影響當前用戶所建立的所有儲存庫區域。

設定檔文件通常位於用戶家目錄下的 .gitconfig 或 .config/git/config

系統級別設定 --system

系統管理員的設定區域,會影響所有用戶所建立的所有儲存庫區域。

設定檔文件通常位於 /etc/gitconfig

健忘筆記

如果想查詢各個區域的設定檔,可以使用 git config --list --show-origin [區域],例如 git config --list --show-origin --global

設定區域優先級

git config 會依照固定的優先順序進行覆蓋。優先順序如下:

  1. --local:對當前資料夾的 Git 儲存庫有效,優先級最高,會覆蓋 --global--system 的相同設定。
  2. --global:對當前用戶的所有 Git 儲存庫有效,優先級次於 --local ,會覆蓋 --system 的相同設定,但不會覆蓋 --local 的。
  3. --system:對系統上所有 Git 的儲存庫有效,優先級最低,會被 --global--local 覆蓋相同的設定。

健忘筆記

優先級順序: --local > --global > --system

git config 常見設定

各個設定都可以依據需求來做不同區域的設定

舉例的中括號 [ ] 為替換字串

git
1
2
3
4
5
6
7
git config [要設定的區域] [甚麼設定] [設定值]

// 設定用戶級別的區域
git config --global user.name "forgetfulengineer"

// 設定當前儲存庫的區域
git config --local user.email "thatforgetfulengineer@gmail.com"

健忘筆記

如果沒有指定設定位置,例如:git config user.name "forgetfulengineer"
git config 的預設位置為 -- local

設定使用者名稱和郵件地址

git
1
2
3
4
5
6
7
8
9
// 設定使用者的名稱
git config --global user.name "[名字]"

git config --global user.name "forgetfulengineer"

// 設定使用者信箱
git config --local user.email "[信箱]"

git config --local user.email "thatforgetfulengineer@gmail.com"

設定 Git 指令縮寫

git
1
2
3
4
5
6
7
8
9
10
11
git config --global alias.[縮寫] [指令]

// 設定 st 等於 status
git config --global alias.st status

// 設定完後使用 git st 等於 git status 的效果

// 設定 br 等於 branch
git config --global alias.br branch

// 設定完後使用 git br 等於 git branch 的效果

設定 Git 使用的編輯器

git config –global core.editor “vim”

git
1
2
3
4
git config --global core.editor "[編輯器]"

// 設定 vim 為 git 的編輯器
git config --global core.editor "vim"

設定 git log 顯示各分支進度

git
1
2
3
4
5
// 開啟顯示各分支進度
git config --global log.decorate auto

// 關閉顯示各分支進度
git config --global log.decorate no

log.decorate 設置有幾個選項:

  • auto:根據終端檢測決定是否顯示各分支進度。
  • short:顯示簡短的各分支進度。
  • full:顯示完整的各分支進度。
  • no:不顯示各分支進度。

git config - 開啟顯示各分支進度

git config - 關閉顯示各分支進度

健忘筆記

HEAD -> dev 表示當前 branch 的進度,當前在 dev 這個 branch 上
origin/dev 表示遠端 branch 的進度,當前 github 上的 dev branch 在 “無障礙功能優化” 這個 commit 上

當前本地 branch 還沒跟遠端 branch 的進度同步

設定 git 追蹤檔案權限

預設情況下修改檔案的權限(chmod) 會影響 git 的紀錄

git
1
2
3
4
5
// 開啟追蹤檔案權限
git config --global core.filemode true

// 關閉追蹤檔案權限
git config --global core.filemode false

範例

command
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// chmod_test.txt 檔案權限是 644
touch chmod_test.txt

// 用 chmod 修改檔案權限
chmod 755 chmod_test.txt

// git diff 查看檔案更動紀錄
git diff chmod_test.txt

// git diff 結果
diff --git a/chmod_test.txt b/chmod_test.txt
old mode 100644
new mode 100755

// 不想記錄檔權限,關閉追蹤檔案權限
git config --global core.filemode false

健忘筆記

三位數的檔案權限數字用來說明三種身分user(自己)、group(組別)、other(其它人)的權限,每一位數是三種權限read(4)、write(2)、execute(1)的加總。
詳細說明看這裡!

設定 git 是否忽略檔名大小寫

預設情況下 git 是忽略檔名大小寫的,所以修改檔名大小寫 git 不會紀錄

git
1
2
3
4
5
// 設定忽略檔名大小寫 (預設)
git config core.ignorecase true

// 設定追蹤檔名大小寫
git config core.ignorecase false

查詢 git config 設定值及刪除設定

查詢單一設定

git
1
2
3
4
5
// 查詢 --global 的使用者名稱
git config --get --global user.name

// 查詢 --local 的 log 設定
git config --get --local log.decorate

查詢所有設定

git
1
2
3
4
5
// 查詢 --global 的所有設定
git config --global --list

// 也可以用縮寫 -l
git config --global -l

健忘筆記

如果沒有指定設定位置,例如:git config --list,會列出當前存儲庫的 Git 配置 --local 和當前用戶級別的 Git 配置 --global,但不包括系統級別的 Git 配置 --system

刪除設定

使用以下命令來刪除 Git 的現有設定:

git
1
2
3
4
5
// 刪除 --global 的使用者名稱
git config --unset --global user.name

// 刪除 --local 的 log 設定
git config --unset --local log.decorate

結論

我認為 git config 中最基本也最重要的兩種設定是 user.nameuser.email,因為使用 git commit 時會記錄名字和信箱,如果沒有設定且嘗試使用 git commit,Git 通常會給出錯誤提示。如果仍然嘗試進行提交,Git 可能會使用空值或電腦的使用者名稱作為提交者資訊,獨自開發的情況下影響不大,但多人開發專案時提交資訊就非常重要了。(才有辦法追朔誰開發出 bug 🤣)

增加一些設定也可以讓我們更加便利,像是設定快捷鍵簡短指令長度,加快打指令的速度,或是讓 git log 顯示分支進度,讓資訊更一目了然。

可能還有很多方便的設定,如果你知道就分享一下吧~

延伸閱讀

作者

健忘工程師

發表於

2024-05-30

更新於

2024-07-29

許可協議


評論

複製完成