文章目錄
git config 是什麼?
git config 是一個記錄了 git 操作的所有基本檔案資料,比方說 git init 創建時預設的 branch 名稱、寫 git commit 的顯示模板、和當 push github 時的使用者資料,都可以在 git config 中調整修改。
git config 的 Scopes:
分成三個層級 System、Global、Local,如果將基本資料寫在 Local 層級的話會覆蓋過其他的層級,而 Global 層級則會覆蓋過 System 層級。
以下是各層級放置的資料位置:
- System: /usr/local/git/etc
- Global: ~/.gitconfig
- Local: .git/config
如何顯示所有的 git 設定
$ git config -l
$ git config --list
$ git config --list --show-origin
有三種寫法,通常我最用的是 git config --list --show-origin
,其中如果有加上 --show-origin
則會列出 file 的位置,可以清楚的區分出是 System、Global 或 Local 層級的 config。
常見的 git config 設定
1. 設定 git 個人資料
可以設定 name 和 email:
$ git config --global user.name "hsuanchi"
$ git config --global user.email [email protected]
2. 設定 git 的編輯器
$ git config --global core.editor vim
3. 設定預設的 branch 名稱
$ git config --global init.defaultBranch main
4. 設定 git commit 的模板
首先將模板路徑寫入 git config
$ git config --global commit.template ~/.gitmessage.txt
創建 gitmessage.txt 模板
$ vim ~/.gitmessage.txt
將以下範上去:
# <type>(<scope>): <subject>
# <BLANK LINE>
# <body>
# <BLANK LINE>
# <footer>
## Allowed <type>
# 1.feat (feature)
# 2.fix (bug fix)
# 3.docs (documentation)
# 4.style (formatting, missing semi colons, …)
# 5.refactor
# 6.test (when adding missing tests)
# 7.chore (maintain)
## Allowed <scope>
# Scope could be anything specifying place of the commit change. For example $location, $browser, $compile, $rootScope, ngHref, ngClick, ngView, etc...
## Message footer
# * Breaking changes
# * Referencing issues: `Closes #123, #245, #992`
## Example
# feat(directive): ng:disabled, ng:checked, ng:multiple, ng:readonly, ng:selected
# New directives for proper binding these attributes in older browsers (IE).
# Added coresponding description, live examples and e2e tests.
# Closes #351
## ref: https://gist.github.com/stephenparish/9941e89d80e2bc58a153
延伸補充:如何優雅的寫好 Git Commit
可以參考 2014 年 Chris Beams 在 How to Write a Git Commit Message 文章中的 Git Commit 七大原則:
- 將訊息用空白行來區分標題與內容
- 標題限制在 50 字元以內
- 標題開頭使用大寫
- 標題不以句點結尾
- 使用祈使句設計標題
- 內容每行最多 72 字,過多文字則需要斷行
- 內容需解釋 what 以及 why vs. how