TL;DR
You may need to run git fetch <upstream>
before running git branch --set-upstream-to <upstream>
.
Today I ran into a similar issue while working on a public repo on Github. I cloned the public repo, made changes locally, then realised that I forgot to first fork this repo on GitHub. So I went ahead and forked it now, but I could not set the local branch to track my fork:
# conventionally it's named 'upstream', but it coincides with git terminology
# so for clarity's sake, I'll name it 'source' in this example
git remote rename origin source
git remote add origin <my-fork>
# errors here:
# fatal: the requested upstream branch 'origin' does not exist
git branch --set-upstream-to origin/master
Basically what's happening is that your local repo isn't aware of the branches in origin
, and indeed git hints that you may wish to run git fetch
first. However a simple git fetch
won't do it, since it automatically fetches from the current default remote, which is still source
.
So what you actually need to do first is either:
# one time
git fetch origin
or
# also set origin as default remote for fetch
git fetch --set-upstream origin
Then your git branch --set-upstream-to ...
should work as expected.