在允许手动指定默认远程仓库的同时,为拉取和推送设置不同的默认远程仓库

3
我有一个本地Git仓库,有两个远程仓库。 originupstream 的一个分支。我在 master 上工作。 我希望实现以下目标:
  • git pull 相当于 git pull upstream master
  • git push 相当于 git push origin master
  • git pull origin masterorigin 拉代码
  • git push upstream master 推到 upstream

因此,将 originupstream 同步的工作流将简化为:

git pull  # from upstream
git push  # to origin

我已经通过一系列git config命令成功配置了第一部分,以下是结果:

[branch "master"]
    remote = upstream
    merge = refs/heads/master
    pushRemote = origin

然而,git push 给我返回了以下错误信息:
fatal: You are pushing to remote 'origin', which is not the upstream of
your current branch 'master', without telling me what to push
to update which remote branch.

有什么想法吗?

你正在使用哪个版本的Git?你是否更改了push.default设置?在Git >= 2.0中,这应该可以工作,除非你配置了其他的push.default设置。 - torek
@torek 我正在使用 Git 2.17,我期望当它不存在时,该值默认为 simple。然而,在明确指定 push.default = simple 后,我的问题得到了解决。感谢您的回复。 - iBug
有趣的是,似乎不需要明确设置push.default就可以工作。我会将其发布为答案。 - torek
2个回答

2

如果您配置正确,Git版本>=2.0实际上应该可以“开箱即用”。

根据评论,显然您还需要明确配置push.defaultsimple,尽管这是未经配置的push.default的默认值。(将push.default设置为current也可以工作,但会删除simple对某些推送添加的安全检查。)

请注意,您可以使用git branch --set-upstream-to设置任何分支的上游:

git branch --set-upstream-to=upstream/master master

但是要实现你想要的功能,需要执行 git config 命令(或直接编辑配置文件——我经常使用 git config --edit 来修正拼写错误),以设置 pushRemote。请注意保留 HTML 标签。


1

虽然不完全符合您的要求,但一种选择是在您的.bashrc文件中为各种命令定义别名:

alias gpull='git pull upstream master'
alias gpush='git push origin master'

然后,就像使用任何普通的UNIX别名一样使用这些别名。我反对尝试改变基本Git命令的语义,部分原因是我不知道如何做,但也部分原因是它可能会在某个时候导致混淆。


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