有沒有常常敲錯命令?好比git status
?status
這個單詞真心很差記。git
若是敲git st
就表示git status
那就簡單多了,固然這種偷懶的辦法咱們是極力同意的。github
咱們只須要敲一行命令,告訴Git,之後st
就表示status
:sql
$ git config --global alias.st status
好了,如今敲git st
看看效果。ruby
固然還有別的命令能夠簡寫,不少人都用co
表示checkout
,ci
表示commit
,br
表示branch
:fetch
$ git config --global alias.co checkout $ git config --global alias.ci commit $ git config --global alias.br branch
之後提交就能夠簡寫成:url
$ git ci -m "bala bala bala..."
--global
參數是全局參數,也就是這些命令在這臺電腦的全部Git倉庫下都有用。spa
在撤銷修改一節中,咱們知道,命令git reset HEAD file
能夠把暫存區的修改撤銷掉(unstage),從新放回工做區。既然是一個unstage操做,就能夠配置一個unstage
別名:3d
$ git config --global alias.unstage 'reset HEAD'
當你敲入命令:code
$ git unstage test.py
實際上Git執行的是:orm
$ 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配置好別名,就能夠輸入命令時偷個懶。咱們鼓勵偷懶。