有沒有經常敲錯命令?比如git status
?status
這個單詞真心不好記。
如果敲git st
就表示git status
那就簡單多了,當然這種偷懶的辦法我們是極力贊成的。
我們只需要敲一行命令,告訴Git,以后st
就表示status
:
$ git config --global alias.st status
好了,現在敲git st
看看效果。
當然還有別的命令可以簡寫,很多人都用co
表示checkout
,ci
表示commit
,br
表示branch
:
$ git config --global alias.co checkout $ git config --global alias.ci commit $ git config --global alias.br branch
以后提交就可以簡寫成:
$ git ci -m "bala bala bala..."
--global
參數是全局參數,也就是這些命令在這台電腦的所有Git倉庫下都有用。
在撤銷修改一節中,我們知道,命令git reset HEAD file
可以把暫存區的修改撤銷掉(unstage),重新放回工作區。既然是一個unstage操作,就可以配置一個unstage
別名:
$ git config --global alias.unstage 'reset HEAD'
當你敲入命令:
$ git unstage test.py
實際上Git執行的是:
$ git reset HEAD test.py
配置一個git last
,讓其顯示最后一次提交信息:
$ git config --global alias.last 'log -1'
這樣,用git last
就能顯示最近一次的提交:
$ git last
commit adca45d317e6d8a4b23f9811c3d7b7f0f180bfe2
Merge: bd6ae48 291bea8
Author: Michael Liao <askxuefeng@gmail.com> Date: Thu Aug 22 22:49:22 2013 +0800 merge & fix hello.py
甚至還有人喪心病狂地把lg
配置成了:
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
來看看git lg
的效果:
為什么不早點告訴我?別激動,咱不是為了多記幾個英文單詞嘛!
配置文件
配置Git的時候,加上--global
是針對當前用戶起作用的,如果不加,那只針對當前的倉庫起作用。
配置文件放哪了?每個倉庫的Git配置文件都放在.git/config
文件中:
$ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true ignorecase = true precomposeunicode = true [remote "origin"] url = git@github.com:michaelliao/learngit.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master [alias] last = log -1
別名就在[alias]
后面,要刪除別名,直接把對應的行刪掉即可。
而當前用戶的Git配置文件放在用戶主目錄下的一個隱藏文件.gitconfig
中:
$ cat .gitconfig [alias] co = checkout ci = commit br = branch st = status [user] name = Your Name email = your@email.com
配置別名也可以直接修改這個文件,如果改錯了,可以刪掉文件重新通過命令配置。
小結
給Git配置好別名,就可以輸入命令時偷個懶。我們鼓勵偷懶。