天天看點

2.7 Git 基礎 - Git 别名

2.7 Git 基礎 - Git 别名

版本說明

版本 作者 日期 備注
0.1 loon 2019.3.21 初稿

目錄

文章目錄

  • ​​2.7 Git 基礎 - Git 别名​​
  • ​​版本說明​​
  • ​​目錄​​
  • ​​Git 别名​​

Git 别名

在我們結束本章 Git 基礎之前,正好有一個小技巧可以使你的 Git 體驗更簡單、容易、熟悉:别名。 我們不會在之後的章節中引用到或假定你使用過它們,但是你大概應該知道如何使用它們。

Git 并不會在你輸入部分指令時自動推斷出你想要的指令。 如果不想每次都輸入完整的 Git 指令,可以通過 git config 檔案來輕松地為每一個指令設定一個别名。 這裡有一些例子你可以試試:

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status      

這意味着,當要輸入 git commit 時,隻需要輸入 git ci。 随着你繼續不斷地使用 Git,可能也會經常使用其他指令,是以建立别名時不要猶豫。

在建立你認為應該存在的指令時這個技術會很有用。 例如,為了解決取消暫存檔案的易用性問題,可以向 Git 中添加你自己的取消暫存别名:

$ git config --global alias.unstage 'reset HEAD --'      

這會使下面的兩個指令等價:

$ git unstage fileA
$ git reset HEAD -- fileA      

這樣看起來更清楚一些。 通常也會添加一個 last 指令,像這樣:

$ git config --global alias.last 'log -1 HEAD'      
$ git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel <[email protected]>
Date:   Tue Aug 26 19:48:51 2008 +0800

    test for current head

    Signed-off-by: Scott Chacon <[email protected]>      
$ git config --global alias.visual '!gitk'