首页 > 解决方案 > Different default remote for pull and push, while allowing manually specifying one

问题描述

I have a local Git repository that has two remotes. origin is a fork of upstream. I work on master. I would like to achieve this:

So the workflow to sync origin with upstream would be simplified to

git pull  # from upstream
git push  # to origin

I've managed to configure the first part with the following result from a series of git config commands:

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

However, git push gives me this error:

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.

Any ideas?

标签: gitgit-pushgit-pullgit-remote

解决方案


这实际上应该在 Git 版本 >= 2.0 中“开箱即用”,即您配置事物的方式。

显然,根据评论,您还必须显式配置push.defaultsimple,即使这是未配置的默认值push.default。(设置push.defaultcurrent也可以,但会删除simple为某些推送添加的安全检查。)

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

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

但它确实需要一个git config命令(或直接编辑配置文件——我喜欢git config --edit经常使用自己,尤其是修复拼写错误)来设置pushRemote你想要实现的目标。


推荐阅读