110

I created a branch called 6796, then I pushed it to remote, checked it out on another machine, made other edits, pushed it, then merged it with master, and deleted it locally and remotely on the other machine (git push :6796). Now, when I run git pull:

fatal: Couldn't find remote ref refs/heads/6796
user@host:~/path/to/repo$ fatal: The remote end hung up unexpectedly

but git pull origin master works normally. It seems to me that there is a 6796 reference somewhere... how to resolve this?

mdesantis
  • 8,257
  • 4
  • 31
  • 63

10 Answers10

210

There are probably some commands to resolve it, but I would start by looking in your .git/config file for references to that branch, and removing them.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
  • 12
    Thank you! I had this line in .git/config: `[remote "origin"] fetch = +refs/heads/6796:refs/remotes/origin/6796` I guess this line is there because I deleted the remote branch and pruned it from another server; so on that server git knew about the deletion, and removed the line in .git/config, but here git was not notified about the deletion. Thanks a lot! – mdesantis Aug 04 '11 at 15:29
  • the `prune` option supplied by @jweyrich is good to "refresh" the origin status – v01pe Jun 12 '13 at 12:31
  • look for .gitconfig on your root directory – Lior Elrom Jul 21 '16 at 00:56
26

You also have to delete the local branch:

git branch -d 6796

Another way is to prune all stale branches from your local repository. This will delete all local branches that already have been removed from the remote:

git remote prune origin --dry-run
jweyrich
  • 31,198
  • 5
  • 66
  • 97
  • that's good way to do it (git remote prune). Is it possible that git will remove something that still exists on remote? I mean is this command dangerous or it only prunes branches that are already deleted from remote – NickSoft Oct 24 '12 at 07:32
  • 2
    @NickSoft, quoting the [official documentation](http://www.kernel.org/pub/software/scm/git/docs/git-remote.html): `Deletes all stale tracking branches under . These stale branches have already been removed from the remote repository referenced by , but are still locally available in "remotes/". With --dry-run option, report what branches will be pruned, but do no actually prune them.` – jweyrich Oct 24 '12 at 07:43
8

In my case, it was the "Lightweight checkout" checkbox that was checked by default, while, as it happens, I needed it unchecked. Can you spot it?

enter image description here

I didn't investigate why did it cause such an abrupt error, but since it blocked my Jenkins jobs for a couple of hours, I felt it's worth sharing on top of the other answers here.

yair
  • 8,945
  • 4
  • 31
  • 50
4

This error could be thrown in the following situation as well.

You want to checkout branch called feature from remote repository but the error is thrown because you already have branch called feature/<feature_name> in your local repository.

Simply checkout the feature branch under a different name:

git checkout -b <new_branch_name> <remote>/feature
ikaruss
  • 491
  • 4
  • 13
3

I just ran into a similar issue when I tried to commit to a newly created repo with a "." in it's name. I've seen several others have different issues with putting a "." in the repo name.

I just re-created the repo and

replaced "." with "-"

There may be other ways to resolve this, but this was a quick fix for me since it was a new repo.

L_7337
  • 2,650
  • 28
  • 42
3

I had a similar issue when I tried to get a pull with a single quote ' in it's name.

I had to escape the pull request name:

git pull https://github.com/foo/bar namewithsingle"'"quote
santiaago
  • 608
  • 9
  • 19
3

To pull a remote branch locally, I do the following:

git checkout -b branchname // creates a local branch with the same name and checks out on it

git pull origin branchname // pulls the remote one onto your local one

The only time I did this and it didn't work, I deleted the repo, cloned it again and repeated the above 2 steps; it worked.

polyccon
  • 681
  • 7
  • 9
3

I had the same issue. But in my case it was due to my branch's name. The branch's name automatically set in my GitHub repo as main instead of master.

git pull origin master 

(did not work).

I confirmed in GitHub if the name of the branch was actually master and found the the actual name was main. so the commands below worked for me.

git pull origin main 
Taterhead
  • 5,763
  • 4
  • 31
  • 40
  • 1
    I can confirm this. The first answer here has more information about transitioning from master to main: https://stackoverflow.com/questions/64249491/difference-between-main-branch-and-master-branch-in-github – aless80 Jun 18 '21 at 08:18
1

I have same error. Problem was that branch was deleted, released. But in PhpStorm I still could see it in remote branches. I could checkout as local branch. And then doing git pull was giving this error.

So need to check if this brnach really exists remotely.

Darius.V
  • 737
  • 1
  • 13
  • 29
0

In my case, it happenned for the master branch. Later found that my access to the project was accidentally revoked by the project manager. To cross-check, I visited the review site and couldn't see any commits of the said branch and others for that project.

Padmanabha V
  • 430
  • 4
  • 11