2013-10-28 51 views
1

我想要一個別名讓git從本地和遠程存儲庫中刪除一個分支。所以,我在創建一個我~/.gitconfig我的Git別名有什麼問題

[alias] 
    erase = !"git push origin :$1 && git branch -D $1" 

它將按預期工作,從源頭和地方分行刪除,但在控制檯中我看到額外的行(error: branch 'profile_endpoints' not found.):

┌[[email protected]:/c/projects/b developing] 
└─$ git erase profile_endpoints 
To [email protected]:a/b.git 
- [deleted]   profile_endpoints 
Deleted branch profile_endpoints (was abcdef0). 
error: branch 'profile_endpoints' not found. 

我在Windows 7上使用git version 1.8.0.msysgit.0git bash

我錯過了什麼?

+0

該分支遠程存在以及本地?運行命令後如何? – iltempo

+0

分支當然存在於本地和遠程回購中。擦除後它被刪除。通過額外的行顯示git試圖做一些奇怪的事情。 – madhead

回答

2

問題是,當你運行一個git別名時,git會在字符串末尾的參數上指向。嘗試,例如:

[alias] 
    showme = !echo git push origin :$1 && echo git branch -D $1 

然後運行:

$ git showme profile_endpoints 
git push origin :profile_endpoints 
git branch -D profile_endpoints profile_endpoints 

有各種不同的解決方法。一個平凡的一個是假設,這將給予一個說法,將追加,所以:

[alias] 
    showme = !echo git push origin :$1 && echo git branch -D 

然而,這個版本增加了誤操作的危險:

$ git showme some oops thing 
git push origin :some 
git branch -D some oops thing 

另一個標準竅門是定義一個shell功能,使所有的上漲,對參數傳遞:

[alias] 
    showme = !"f() { case $# in 1) echo ok, $1;; *) echo usage;; esac; }; f" 

$ git showme some oops thing 
usage 
$ git showme one 
ok, one 

一個,這是一個有點iffier是使用一個假的「吸收額外的參數」命令:

[alias] 
    showme = !"echo first arg is $1 and others are ignored; :" 

$ git showme one two three 
first arg is one and others are ignored 

我自己的規則是一旦別名變得複雜,就切換到「真正的」shell腳本。 :-)

+0

謝謝澄清! – madhead