簡體   English   中英

我的Git別名怎么了

[英]What's wrong with my Git alias

我想要git的別名從本地和遠程存儲庫中刪除一個分支。 因此,我在~/.gitconfig創建了一個:

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

它按預期工作,從源和本地刪除分支,但在控制台中我看到多余的行( error: branch 'profile_endpoints' not found. ):

┌[madhead@MADHEAD-LAPTOP:/c/projects/b developing]
└─$ git erase profile_endpoints
To git@github.com: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

我想念什么?

問題是,當您運行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

稍微麻煩一點的是使用虛擬“吸收額外參數”命令:

[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腳本。 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM