141

I'm not sure why this doesn't work. When I do git branch -a, this is what I see:

enter image description here

I'm trying to pull from the DownloadManager on the online GitHub repository. I have tried

  • git pull, but then it complains about not knowing which branch to pull from
  • git pull origin, doesn't know which branch
  • git pull origin downloadmanager gives fatal: Couldn't find remote ref downloadmanager. Unexpected end of commands stream
  • git pull origin remotes/origin/DownloadManager gives fatal couldn't find remote ref remotes/origin/DownloadManager. Unexpected end of commands stream

Is there something I'm missing? In Xcode, When I try to connect to the repository, nothing ever shows up. I have been able to push to it in the past. But I can't push again until I pull the most recent changes.

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
Crystal
  • 28,460
  • 62
  • 219
  • 393
  • 9
    How about `git pull origin DownloadManager`? – Felix Kling Jul 19 '12 at 00:43
  • Is your local downloadmanager branch a tracking branch of the remote download manager? Here's how you can [check](http://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking). – Roman Jul 19 '12 at 01:23
  • @Felix Kling That worked. Do you want to answer so I can have my question answered? So it's just case sensitive? – Crystal Jul 19 '12 at 01:28
  • for all looking for another answer to the question `git pull remote branch cannot find remote ref`: Check if your remote branch was deleted. To be sure having the current state locally, do a `git fetch` with the `--prune origin` flag – chrissharkman Jan 28 '20 at 09:22
  • "git fetch --prune" will fail if the remote branch was deleted/renamed. Try to unset remote refs with `git config --unset-all remote.origin.fetch` . See: https://stackoverflow.com/a/67200162/658497 – Noam Manos Apr 22 '21 at 06:17
  • Make sure the remote branch exists in the first place. – shasi kanth Dec 14 '21 at 15:06

21 Answers21

125

Be careful - you have case mixing between local and remote branch!

Suppose you are in local branch downloadmanager now (git checkout downloadmanager)

You have next options:

  1. Specify remote branch in pull/push commands every time (case sensitive):

    git pull origin DownloadManager

    or

    git pull origin downloadmanager:DownloadManager


  1. Specify tracking branch on next push:

    git push -u origin DownloadManager

    (-u is a short form of --set-upstream)

    this will persist downloadmanager:DownloadManager link in config automatically (same result, as the next step).


  1. Set in git config default remote tracking branch:

    git branch -u downloadmanager origin/DownloadManager

    (note, since git 1.8 for branch command -u is a short form of --set-upstream-to, which is a bit different from deprecated --set-upstream)

    or edit config manually (I prefer this way):

    git config --local -e

    -> This will open editor. Add block below (guess, after "master" block):

    [branch "downloadmanager"]
            remote = origin
            merge = refs/heads/DownloadManager
    

and after any of those steps you can use easily:

git pull

If you use TortoiseGit: RightClick on repo -> TortoiseGit -> Settings -> Git -> Edit local .git/config

radistao
  • 14,889
  • 11
  • 66
  • 92
  • 5
    Just as an update, nowadays `--set-upstream` is deprecated in favor of `--set-upstream-to` or `--track` – Alessandro Fazzi Feb 24 '15 at 13:42
  • 1
    Thanks Man, its a big help. I had not time when I had to show u my code & already messed up with previous repository. Thanks again. – Ozzius Aug 26 '20 at 18:43
30

This error happens because of local repository can't identify the remote branch at first time. So you need to do it first. It can be done using following commands:

git remote add origin 'url_of_your_github_project'

git push -u origin master
UWU_SANDUN
  • 1,123
  • 13
  • 19
24

If none of these answers work, I would start by looking in your .git/config file for references to the branch that makes problems, and removing them.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
rubo77
  • 19,527
  • 31
  • 134
  • 226
18

If the remote branch was deleted (or renamed), then you might get such error when trying to fetch that old-branch:

$ git fetch --prune --all

  Fetching origin
  fatal: couldn't find remote ref refs/heads/old-branch
  error: Could not fetch origin

Check your local git config if it still refers to the old-branch:

  $ git config --get-all remote.origin.fetch

    +refs/heads/*:refs/remotes/origin/*
    +refs/heads/old-branch:refs/remotes/origin/old-branch
    +refs/heads/master:refs/remotes/origin/master

Removing the old refs entries, can fix the fetch problem:

$ git config --unset-all remote.origin.fetch

$ git fetch --prune --all
  Fetching origin
  ...
   * branch            HEAD       -> FETCH_HEAD
Noam Manos
  • 15,216
  • 3
  • 86
  • 85
17

I faced the same issue because GitHub changed the default branch name from master to main so git pull origin master did not work for me.

try this
git pull origin main If the issue is due to a branch name conflict, this will save you.

Saurabh Mahra
  • 330
  • 5
  • 10
12

The branch name in Git is case sensitive. To see the names of your branches that Git 'sees' (including the correct casing), use:

git branch -vv

... and now that you can see the correct branch name to use, do this:

git pull origin BranchName 

where 'BranchName' is the name of your branch. Ensure that you match the case correctly

So in the OP's (Original Poster's) case, the command would be:

git pull origin DownloadManager
Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
11

For me, it was because I was trying to pull a branch which was already deleted from Github.

Nilesh leve
  • 111
  • 1
  • 4
5

In my case, this error occurred due to the naming changes made by Github for the default branch from master to main

So instead of using,

git pull origin master

You may use,

git pull origin main
Niroshan Ratnayake
  • 3,433
  • 3
  • 20
  • 18
4

check your branch on your repo. maybe someone delete it.

joshua pogi 28
  • 521
  • 1
  • 6
  • 15
3

You need to set your local branch to track the remote branch, which it won't do automatically if they have different capitalizations.

Try:

git branch --set-upstream downloadmanager origin/DownloadManager
git pull

UPDATE:

'--set-upstream' option is no longer supported.

git branch --set-upstream-to downloadmanager origin/DownloadManager
git pull
olajide
  • 972
  • 1
  • 13
  • 26
Matt Gibson
  • 14,616
  • 7
  • 47
  • 79
2

In my case I had got the capitalisation wrong on the branch ABC-100 when I did git checkout abc-100 which I then worked on. My solution was to merge code from abc-100 into ABC-100 and then delete abc-100.

Tristanisginger
  • 2,181
  • 4
  • 28
  • 41
2

Hiii, I just solved this problem thanks to these comentaries.I'm a newbie so just a tiny addition since this question was solved 7 years ago the new standard for the "the principal bratch" in git and github is no longer master but rather main this is generating some broke parts on stackoverflow for old code, you can the Github announcement about that here: Renaming the default branch from master

Because of that first verify with what name are managing your "principal branch" with git show-ref assuming you are using Git Bash. If you are watching an old tutorial or old book this might be the cause.

what should appear should be:

$ git show-ref
3fba1252f165a909f14dd63e1177c543659dffd6 refs/heads/**main**
9264e8f4193fa1be7ccb5780059bc67f68cf107e refs/remotes/origin/**main**

if you see master in one of those change it to main, if not skip this part:

$ git branch -m master main
$ git push -u origin main

Anyway worked for me was this:

$ git pull origin main --allow-unrelated-histories 
$ git push -u origin main 
hiyorijl
  • 56
  • 5
1

This is because your remote branch name is "DownloadManager“, I guess when you checkout your branch, you give this branch a new name "downloadmanager".

But this is just your local name, not remote ref name.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Tim
  • 2,006
  • 2
  • 18
  • 25
1

For me it was failing because my remote branch was missing (it was deleted on previous merge run.)

Minions
  • 27
  • 9
1

In my case I had a branch in my local branches that was removed from the server

just remove it from the local branches and pull will works as expected

Alex
  • 8,908
  • 28
  • 103
  • 157
1

The problem occured for me because (I think) someone tried to clone a repository from a folder on our server into the same folder. This created a ".git" folder in the repository I tried to clone and broke everything. Deleting the ".git" folder in the repo I wanted to clone fixed it.

1

It's possible that you created a new repo and did not have a readme.md created with it. This can result in the online repo not having a default branch set. You can manually add a readme.md in github or gitlab and then a master/main branch should be created.

You may also be able to set a default branch in the settings. In my case I was not able to do the latter so I had to do the first and then I was able to push to origin -main.

1

This error on a norms is caused when there's a spelling mistake in the branch you're trying to pull from or if the branch name doesn't exist.

It's recently that GitHub changed the default branch name from master to main.

In my case, this wasn't the case.

I'm using a branch name that is same as my username. I then had to first push to the branch, then make a pull request which got merged from my branch to a complete and updated branch, the pull requests I started making afterwards works fine.

0

I had this issue when after rebooted and the last copy of VSCode reopened. The above fix did not work, but when I closed and reopened VSCode via explorer it worked. Here are the steps I did:

//received fatal error
git remote remove origin
git init
git remote add origin git@github:<yoursite>/<your project>.git
// still received an err 
//restarted VSCode and folder via IE 
//updated one char and resaved the index.html  
git add .
git commit -m "blah"
git push origin master
0

In the old projects (before Oct 2020) the name of the 'main' branch used to be 'master'.

I was trying to do a 'git pull origin main' whereas my branch name was 'master'.

So I tried git pull origin master , and I was there.

navinrangar
  • 904
  • 10
  • 20
-1

In my own case, I just made the change from "master" to "main" and I was able to pull from the remote repo.

Justobioma
  • 11
  • 2