将当前分支推送到Gerrit

8

我喜欢使用简单的git push命令将当前分支推送到配置的上游。为此,我设置了git config push.default upstream,它会自动做正确的事情。

现在,我尝试为Gerrit设置适当的远程,以便自动推送到refs/for/*

做法如下:

[remote "gerrit"]
        url = ssh://host:port/repo
        push = refs/heads/*:refs/for/*

我想输入git push gerrit,并期望git仅将当前分支推送到正确的引用。不幸的是,现在git会尝试将所有本地分支推送到Gerrit。-是否有办法告诉git只像上面的push.default一样推送当前分支?
更新:
  • Git push仅能使用一个refspec。
  • 如果在命令行上给出了显式的refspec,则使用该值。
  • 如果没有在命令行上给出,则使用与远程定义的refspec。
  • 如果没有与远程定义,则仅考虑push.default。
对于Gerrit,我需要类似'branch:refs/for/branch'的refspec,其中branch是当前分支。
问题:
  • 是否有与push.default=upstream相当的显式refspec?
  • 是否有一种方法可以配置git使纯粹的git push自动推送到正确的refs/for ref?
目前我发现的最好方法是将所有内容封装在单独的脚本中,就像这里建议的那样。但我仍然不相信仅使用适当的refspec是不可能的。

好的。只是在这里检查不同的选项。我已经了解到,只有在没有给出refspec时才考虑push.default。如果您通过命令行提供refspec(如HEAD),则会覆盖与远程给定的refspec。因此,它将只推送到ref/heads,这不是您想要的。有趣的。 - VonC
3个回答

1
根据来源,Git目前通过生成包含显式名称的显式refspec来实现push.default = upstream
看起来目前没有办法通过单个通用refspec指定该行为,而一些脚本(如建议的此处)构建显式refspec是目前唯一的方法。

有趣的反馈,肯定比我(现在已删除的)回答更准确。+1 - VonC

1
作为一种解决方法,我拦截了git函数并让它执行默认的push操作:
.bashrc
git ()
{
    if [ $1 = "push" -a -z $2 ]
    then
        /usr/bin/git push origin HEAD:refs/for/master
    else
        /usr/bin/git $@
    fi
}

我猜这是个hack,但对我来说有效。


-1

你可以使用命令 git config --global push.default current 配置 git 仅将当前分支发送到远程仓库。

你可以传递给 push.default 的其他选项包括:

nothing - do not push anything.
matching - push all matching branches. All branches having the same name in both ends are considered to be matching. This is the default.
upstream - push the current branch to its upstream branch.
tracking - deprecated synonym for upstream.
current - push the current branch to a branch of the same name.

这些配置可以在git help config命令中查看。

更新:然而,仔细阅读手册页面,push.default“定义了如果命令行上没有给出refspec,远程没有配置refspec,并且命令行上给出的任何选项都没有暗示refspec,则git push应该采取的操作”。因此,我认为当指定refspec时,您需要使用脚本仅发送当前分支。


你在发帖前看了我上面的评论吗?我在下面回答了同样的问题(已删除,你看不到),结果得到了-1的评分 ;) - VonC
你完整地阅读了这个问题吗?不,只有在没有给出refspec的情况下才会考虑push.default,也就是说,它只会推送到heads。 - michas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接