git配置別名

來源:互聯網
上載者:User

標籤:

配置別名 

有沒有經常敲錯命令?比如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 lastcommit adca45d317e6d8a4b23f9811c3d7b7f0f180bfe2Merge: bd6ae48 291bea8Author: Michael Liao <[email protected]>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配置好別名,就可以輸入命令時偷個懶。我們鼓勵偷懶。

 

git配置別名

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.