2013-05-05 82 views
2

問題元別名Git中的Bash

我很好奇,想知道是否可以在Git的製作別名,即元別名創建一個別名,以便不用打字git config --global alias. -whatever一個能只要寫git alias - 無論如何。

背景:

  1. 我只升溫到Bash和Git的,並且最近已經開始創建別名像git cfggit config --global和,from this pagegit hist爲:

    git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short 
    
  2. 最後一個例子通過鍵入:

    git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short' 
    
  3. 隨着我git cfg別名我可以縮短,爲:

    git cfg alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short' 
    

但我想弄清楚,如果我能縮短它更進一步使git cfg alias.只是git alias,部分出方便,部分出於好奇,部分原因是想了解更多關於Git和Bash的內容。

嘗試的解決方案?:

  1. 我發現我可以打電話git $(echo 'cfg'),並得到相同的結果git cfg。希望我能像鍵入一些git alias st status創建一個快捷別名git stgit status

    git cfg alias.alias '$(echo "config --global alias.")' 
    

  2. 所以,我想通過輸入此創建別名爲alias

  3. 不幸的是,這似乎並沒有在這一點上的Git不再承認$(echo的命令,因爲工作,即使它的工作別名的背景之外,與git $(echo 'cfg')。我也不確定是否工作,因爲示例中的st必須從執行的回顯中直接附加到alias.

有沒有人有任何建議,如何使這成爲可能?

回答

1

git wiki

Now that you know all about aliases, it might be handy to define some, using an alias: 

    [alias] 
      alias = "!sh -c '[ $# = 2 ] && git config --global alias.\"$1\" \"$2\" && exit 0 || echo \"usage: git alias <new alias> <original command>\" >&2 && exit 1' -" 

then define new aliases with: 

    $ git alias new_alias original_command 

表達[ $# = 2 ]是殼表達式,如果參數的數目等於二這是真的。

的結構是這樣的:

not <shell cmd> AND exit 0 OR <display syntax> AND exit 1 

這是shell語言,如果參數的數量爲兩個,和git返回成功,則返回成功(0)。否則,打印語法錯誤消息,並返回失敗(1)。

+0

這不僅工作,但解釋也有幫助:-)謝謝! – guypursey 2013-05-06 09:26:45