32

I am attempting to follow some steps to contribute to a repository on GitHub and one of the steps is not working. The steps are here: https://github.com/wdbm/qTox/blob/master/CONTRIBUTING.md#how-to-open-a-pull-request.

I fork the repository on GitHub.

I clone the repository:

git clone https://github.com/<YOUR_USER>/qTox.git

I access the directory of the local repository:

cd qTox

I add the upstream remote in order to be able to fetch from the upstream repository:

git remote add upstream https://github.com/qTox/qTox.git

I attempt to point the local master branch to the upstream repository:

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

This command fails with the following error message:

error: the requested upstream branch 'upstream/master' does not exist
hint: 
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint: 
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.

How should I address this error? I am using Git 2.9.3.

Kara
  • 6,115
  • 16
  • 50
  • 57
d3pd
  • 7,935
  • 24
  • 76
  • 127
  • 3
    did you run "git fetch", as the hint says? also, what branch are you in locally? – zim Dec 31 '16 at 23:48
  • @zim Thanks for your suggestion. I tried running `git fetch`, but I still get the same error when I run `git branch master --set-upstream-to=upstream/master`. I am attempting to following the steps described [here](https://github.com/wdbm/qTox/blob/master/CONTRIBUTING.md#how-to-open-a-pull-request). – d3pd Jan 01 '17 at 00:16
  • 1
    can you show the output from these two commands: "git status" and "git remote -v"? – zim Jan 01 '17 at 00:24
  • @zim `git status` shows "On branch master Your branch is up-to-date with 'origin/master'." and `git remote -v` shows origin (both fetch and push) as `https://github.com//qTox.git` and upstream (both fetch and push) as `https://github.com/qTox/qTox.git`. – d3pd Jan 01 '17 at 00:55
  • 1
    good, that looks rights. check out the accepted answer here: http://stackoverflow.com/questions/22080952/git-creating-a-branch-which-will-be-pushed-to-a-remote-later – zim Jan 01 '17 at 01:01
  • 1
    @zim Sorry, I'm lost. When I try the next step in the documentation (`git fetch upstream master:master`) I get the error message "fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository" which does not sound right. – d3pd Jan 01 '17 at 01:29

4 Answers4

35

git fetch upstream master:master: this work only when you are not on master. If you are on master, a simple git fetch upstream is enough.

Then you can link your local master to the remote tracking branch upstream/master (which has just been fetched)

git branch -u upstream/master master

Then you can use git pull to update master.
Again, if you are not on master, then yes, git fetch upstream master:master will work.


Luh_ mentions also a typo issue in the refspec: see "git fetch doesn't fetch all branches".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    `git fetch upstream` was what did it for me on my `master` branch! I kept doing `git fetch`, like the hint said, but that did nothing. git's hints could really be a bit more specific(=practical) sometimes. Thank you! – K.Sy Jun 05 '23 at 08:31
  • 1
    @MaximizedAction True, the remote name is important in this case. Without it, Git would use by default the name "`origin`". – VonC Jun 05 '23 at 08:49
10

I had a similar problem, however git fetch didn't solve my problem. Also, in my case I found that git config --get remote.origin.fetch didn't return anything while it is suppose to

My problem was that there was a typo in the .git/config file in the fetch line of the respective remote block (probably something I added by mistake previously). So, check if your remote entry in the .git/config file is correct, e.g.:

[remote "origin"]
    url = https://[server]/[user or organization]/[repo].git
    fetch = +refs/heads/*:refs/remotes/origin/*

You can also directly remove and re-add the remote entry

Juh_
  • 14,628
  • 8
  • 59
  • 92
  • 1
    Thanks, after hours trying to fix this error, I dicover that the old developer had left this config wrong – renanrider Jun 07 '22 at 19:13
0

Maybe in latest version of git the help text is out-dated.

I was running into this:

$ git pull origin master
From github.com:TextUsBiz/tesseract
 * branch                master     -> FETCH_HEAD
Already up to date.

$ git branch --set-upstream-to=origin/master master
fatal: the requested upstream branch 'origin/master' does not exist
hint: 
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint: 
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.
hint: Disable this message with "git config advice.setUpstreamFailure false"

I thought the error message was quite odd, but.. I tried this, and it worked!

$ git branch --set-upstream-to=master master
branch 'master' set up to track 'origin/master'.
Devin Rhode
  • 23,026
  • 8
  • 58
  • 72
-6

Try this

git branch -u git branch --set-upstream-to=<remote>/<remote branch> branch