2013-02-27 122 views
15

我想改進當前的別名,其中大多數都在分支上工作。 有沒有辦法在git別名中引用當前分支,所以我不需要每次都傳遞它?當前分支上的Git別名

像這樣的:

git config alias.po "push origin" 

是使用這樣的:

git po foo_branch 

我想只要運行git po,並採取當前分支作爲參數。

+2

git rev-parse --abbrev-ref HEAD是我用來驅動當前分支的東西。 – Learath2 2013-02-27 12:46:25

回答

0

這個答案將是有效的混帳2.0,其中默認的推動行爲將simple

除非push.default設置爲matchinggit push沒有指定參數將始終推動當前分支開始,所以在這你不需要指定它。

+2

-1:我不認爲這是正確的。 'git push'的默認動作取決於'.gitconfig'中的'push.default'設置。 – lunaryorn 2013-02-27 12:43:04

+0

是的,但只有它被設置爲「匹配」 – CharlesB 2013-02-27 12:57:16

+0

實際上'matching'是'push.default'的當前默認值,所以我改變了我的回答 – CharlesB 2013-02-27 13:00:24

0

從您的問題中,您不需要明確說明這兩個別名中的哪一個。

這將推動當前已簽出分支:

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 
+1

使用'git symbolic-ref HEAD'來獲取當前分支。應該更快。 – lunaryorn 2013-02-27 12:45:35

+0

@ lunaryorn爲什麼不'git rev-parse --abbrev-ref HEAD'?它返回分支名稱沒有參考 – vladkras 2016-07-06 09:21:07

2

我不知道任何內置的方式,但你可以用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無恥地採取。

34
[alias] 
    po = "!git push --set-upstream origin \"$(git rev-parse --abbrev-ref HEAD)\"" 
+0

很好的答案!唯一需要注意的是,我直接從命令行執行此操作,並將當前分支硬編碼到命令行中:) Direclty在配置文件中做了它,它工作得很好。 – CubanX 2014-02-08 15:30:30

+1

爲什麼在'!git'中爆炸?我知道什麼是爆炸 - 什麼在bash中,只是不知道你爲什麼要在這裏。 – 2014-05-22 20:14:22

+3

bang允許您執行shell中的所有內容,即允許您執行任何命令,而不僅僅是別名中的git命令。 – 2015-03-04 17:37:53