我想改進當前的別名,其中大多數都在分支上工作。 有沒有辦法在git別名中引用當前分支,所以我不需要每次都傳遞它?當前分支上的Git別名
像這樣的:
git config alias.po "push origin"
是使用這樣的:
git po foo_branch
我想只要運行git po
,並採取當前分支作爲參數。
我想改進當前的別名,其中大多數都在分支上工作。 有沒有辦法在git別名中引用當前分支,所以我不需要每次都傳遞它?當前分支上的Git別名
像這樣的:
git config alias.po "push origin"
是使用這樣的:
git po foo_branch
我想只要運行git po
,並採取當前分支作爲參數。
這個答案將是有效的混帳2.0,其中默認的推動行爲將simple
除非push.default
設置爲matching
,git push
沒有指定參數將始終推動當前分支開始,所以在這你不需要指定它。
從您的問題中,您不需要明確說明這兩個別名中的哪一個。
這將推動當前已簽出分支:
git config alias.po !f() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git push origin $tmp_branch; unset $tmp_branch; }; f
這將推動這個分支名稱(git po branchName
):
git config alias.po !f() { git push origin $1; }; f
我不知道任何內置的方式,但你可以用shell別名做:
alias gpo='git push origin "$(git-current-branch 2> /dev/null)"'
其中git-current-branch
被定義爲f ollows:
git-current-branch() {
if ! git rev-parse 2> /dev/null
then
print "$0: not a repository: $PWD" >&2
return 1
fi
local ref="$(git symbolic-ref HEAD 2> /dev/null)"
if [[ -n "$ref" ]]
then
print "${ref#refs/heads/}"
return 0
else
return 1
fi
}
無恥地從Prezto無恥地採取。
[alias]
po = "!git push --set-upstream origin \"$(git rev-parse --abbrev-ref HEAD)\""
很好的答案!唯一需要注意的是,我直接從命令行執行此操作,並將當前分支硬編碼到命令行中:) Direclty在配置文件中做了它,它工作得很好。 – CubanX 2014-02-08 15:30:30
爲什麼在'!git'中爆炸?我知道什麼是爆炸 - 什麼在bash中,只是不知道你爲什麼要在這裏。 – 2014-05-22 20:14:22
bang允許您執行shell中的所有內容,即允許您執行任何命令,而不僅僅是別名中的git命令。 – 2015-03-04 17:37:53
git rev-parse --abbrev-ref HEAD是我用來驅動當前分支的東西。 – Learath2 2013-02-27 12:46:25