Search code examples
gitgit-pushgit-pullgit-remote

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:

  • git pull is equivalent to git pull upstream master
  • git push is equivalent to git push origin master
  • git pull origin master pulls from origin
  • git push upstream master pushes to upstream

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?


Solution

  • This is actually supposed to work "out of the box" in Git versions >= 2.0, the way you configured things.

    Apparently, per comments, you also have to explicitly configure push.default to simple, even though that's the default for an unconfigured push.default. (Setting push.default to current would also work, but would remove the safety check that simple adds for some pushes.)

    Note that you can set the upstream of any branch using git branch --set-upstream-to:

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

    but it does take a git config command (or direct editing of the config file—I like to use git config --edit myself often, especially to fix typos) to set the pushRemote as well, which you need for what you want to achieve.