章節 11 別名
學習目標
- 學習如何設定 git 指令的別名(Alias) 和 捷徑
一般指令別名 01
git status, git add, git commit, 和 git checkout 這些是很常使用的指令,簡化指令將對使用上很有幫助。
在您$HOME目錄下編輯 .gitconfig 檔案加入下面的設定:
檔案: .gitconfig
[alias] co = checkout ci = commit st = status br = branch hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short type = cat-file -t dump = cat-file -p
我們已經設定了包含 checkout, commit 和 status 的縮寫. 加上應用上一章節 log 指令的參數變化。 現在您可以輸入 git co 等同於輸入 git checkout. 其它像是 git st 對應 git status 以及 git ci 對應 git commit。 最棒的是 git hist 您可以使用像上一章節旗艦版的指令卻不需要輸入一長串 log 指令加參數。
繼續來試試看其他新指令。
在.gitconfig 定義 hist 別名來查看歷史紀錄 02
在其他章節和大部分的情況下,我將在文章中繼續使用完整的指令。唯一的例外是我會使用 hist 來查看 log 歷史訊息。 所以確認您已經設定 hist 別名在您的 .gitconfig
Type 和 Dump 03
同時我們已經加入一些其他我們還沒用到的別名。 我們很快會用到 git branch 還有 git cat-file 指令這些常用來瀏覽查看 git 的指令。
命令列別名(Unix Like 選用)04
注意: 這個部分是針對 posix-like shell 的使用者。 Windows 的使用者和其它沒有 Shell 的使用者可以跳過這個部分。 這部分只是要讓常用的指令更加精簡。 Windows使用者若要使用可使用 Cygwin 。
如果您的 shell 支援別名或捷徑那麼您也可以使用。這裡提供一下我使用的設定:
檔案: .profile
alias gs='git status ' alias ga='git add ' alias gb='git branch ' alias gc='git commit' alias gd='git diff' alias go='git checkout ' alias gk='gitk --all&' alias gx='gitx --all' alias got='git ' alias get='git '
git checkout 縮寫成 go 恰巧特別有含義。 :
go <branch>
可以切換到指定的分支(branch)
而且我常常誤把 git 打成 get 或 got 所以我也把這個常犯的錯誤加入設定,當我打成 got 或 get 時還是會執行 git。