目的
學(xué)習(xí)如何設(shè)置別名及簡寫 Git 命令。
git status、git add、git commit、git checkout 是非常常用的命令,因此對(duì)它們進(jìn)行縮寫十分有用。
添加下列內(nèi)容到你的 $HOME 目錄的 .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
我們已經(jīng)介紹了 commit 和 status 命令。并且在上一實(shí)驗(yàn)中也介紹了 log 命令。checkout 命令將接下來介紹。
使用這些在 .gitconfig 中定義的別名,你可以通過輸入 git co來表示 git checkout。同時(shí),git st 表示 git status,而git ci 表示 git commit。并且,最好的是 git hist 將使你避免很長的 log 命令。
去試試新命令吧。
在大多數(shù)介紹中,我將繼續(xù)輸入完整的命令。唯一的例外是,當(dāng)我需要看 git log 的輸出時(shí),我將使用上面定義的 hist 別名。如果你想要遵循這里,那么在繼續(xù)前設(shè)置你的 .gitconfig 文件。
我們已經(jīng)添加了幾個(gè)還沒有介紹的命令別名。git branch 命令很快將介紹。git cat-file 命令對(duì)于瀏覽 Git 很有用,一會(huì)兒我們將看看。
注意:本小節(jié)是為那些運(yùn)行 POSIX 類 Shell 的同學(xué)寫的。Windows用戶及其他非 POSIX Shell 用戶可以跳到下一個(gè)實(shí)驗(yàn)。
如果你的 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>
來檢出一個(gè)特定的分支。
另外,我也經(jīng)常通過創(chuàng)建足夠的別名來避免打錯(cuò) Git 命令。
注意:有些 Shell 別名有點(diǎn)攻擊性。實(shí)際上,gs 將與 Linux GhostScript 程序沖突。最近我開始使用 Go 編程語言,因此必須禁用上面的 go 別名。所以使用這些別名要小心。