266

Full message:

error: Ref refs/remotes/origin/user is at 3636498c2ea7735fdcedc9af5ab3c8689e6abe77 but expected a21359c6cc2097c85775cde6a40105f4bd7100ec
From github.com:{github project url}
 ! a21359c..6273ffc  user -> origin/user  (unable to update local ref)
Sanjeev Kumar Dangi
  • 5,915
  • 5
  • 25
  • 30

16 Answers16

248

If you are running git under a file system that is not case sensitive (Windows or OS X) this will occur if there are two branches with the same name but different capitalisation, e.g. user_model_changes and User_model_changes as both of the remote branches will match the same tracking ref.

Delete the wrong remote branch (you shouldn't have branches that differ only by case) and then git remote prune origin and everything should work

sunkehappy
  • 8,565
  • 5
  • 36
  • 60
krijesta
  • 2,607
  • 1
  • 12
  • 3
  • thanks for answer. This wasn't the issue with this error. I already found the solution to this issue and posted in comment below the question. – Sanjeev Kumar Dangi Aug 23 '12 at 12:31
  • 2
    Helps for Mac OS X as well (the default filesystem is case insensitive). – Tammo Freese Sep 09 '13 at 09:27
  • 20
    yes, the case-insensitive issue on Windows caused the problem. I fixed it by manually removing the ref in `.git\refs\remotes\origin` folder and then `git pull` again. – Roy Ling Apr 13 '15 at 05:14
  • 1
    and for those who dont know where .git folder will be.. it will be created inside your Project/workspace folder :D – kumar Oct 11 '17 at 11:56
  • 1
    This is a git bug.(At least the error message is wrong). I hope someone can report this bug to the git project. It looks difficult for me to report bug to the git project. https://github.com/git/git – bronze man Jan 30 '18 at 02:12
  • how does one "Delete the wrong remote branch " ? `git branch -d a_remote_branch_name` says that the branch is not found – nass Sep 05 '18 at 09:35
225

Permanent Fix

git update-ref -d resolved my instance of this error, e.g.

git update-ref -d refs/remotes/origin/user

Note that this doesn't impact remote.

In my case, a subsequent git fetch fetched that branch again, and following git fetches/pulls no longer gave the error "remote ref is at but expected".

If that doesn't work, a temporary fix:

Note also that if you don't care about the branch in question (e.g. you just want to update master, not origin/user), a git pull workaround is to fetch then just merge the particular branch you care about, e.g.

git fetch # may give an error for a particular branch, but other branches will still be successfully fetched
git merge origin/master
JDiMatteo
  • 9,062
  • 3
  • 47
  • 55
  • 1
    This should be the accepted solution because it solves the problem without touching remote. – Cerno May 29 '20 at 13:30
  • This just moved me further into the swap. Doing `git rebase --continue` afterwards I get `fatal: Needed a single revision. Cannot read HEAD` – Axel Bregnsbo Sep 16 '20 at 13:00
  • Best answer (permanent fix). You are not touching remote and a subsequent git pull gets the branch again. – kratos Apr 13 '21 at 12:11
69

Just delete the folders and files under \.git\refs\remotes\origin. Works, when u don't have unpushed changes.

Swapnil Patwa
  • 3,871
  • 3
  • 22
  • 34
  • This might not work if your remote ref is 'packed' and therefore not in refs/remotes/** @JDiMatteo's solution should work then still – Alexander Bird Dec 07 '16 at 17:30
  • 2
    Worked for me. Thanks a ton! – Swapnil Patwa Mar 22 '19 at 07:55
  • 1
    When I do this, and then do a pull, all those deleted files under `\.git\refs\remotes\origin` returns back and in the subsequent pull, I get the same error. It is a cycle. What I did was to just delete the local copy and clone a fresh copy. That worked for me. – Arjun Kalidas Sep 08 '20 at 17:10
60

Use the below two commands one by one.

git gc --prune=now

git remote prune origin

This will resolve your issue.

Narender Gusain
  • 1,801
  • 11
  • 13
54

I ran this to solve the problem:

git gc --prune=now
elad silver
  • 7,348
  • 3
  • 38
  • 57
12

I had to remove my branch from my command line at:

.git\refs\remotes\{my remote}\{**my branch**}

and then manually doing:

git pull [remote_name] [branch_name]

I was able to pull the changes.

Note: I was using SourceTree and was unable to do the pull.

Kevin
  • 40,904
  • 12
  • 48
  • 67
jogam5
  • 166
  • 1
  • 4
  • Ultimately I renamed my remotes: I had two remotes "Bitbucket/staging" and "bitbucket/staging" in my SourceTree history but only "Bitbucket" appeared on the command line when doing: git remote -v. So I renamed Bitbucket to bitbucket and the conflict finally disappeared, hope this helps most likely SourceTree users. – jogam5 Mar 31 '16 at 20:51
  • I also had to remove the branch from `.git\packed-refs` before it was fixed for me. – Michael May 11 '18 at 02:42
7

A hard reset will also resolve the problem

git reset --hard origin/master
mruanova
  • 4,119
  • 3
  • 30
  • 46
  • Have you selected a best answer? – mruanova Aug 22 '17 at 13:44
  • 1
    this is the best and easiest answer i think. git reset --hard – soung Dec 15 '20 at 20:52
  • 1
    While this will resolve the problem, I'd also like to caution users that executing this command when you have local commits in the branch that are not yet pushed to remote will essentially wipe out those local commits (however, these can still be retrieved using git reflog for a period of time) – Ram May 12 '21 at 00:21
5

Clearer Steps

  1. in terminal

    cd /.git/refs/remotes/origin
    
  2. do ls, you will see some branches and HEAD

  3. Remove the branch you think has the problem

    rm branchname
    
  4. If it did not work, delete all branches/HEAD

    • you may wana pull

Hope it works now.

Emile Bergeron
  • 14,368
  • 4
  • 66
  • 111
shareef
  • 7,886
  • 12
  • 53
  • 80
4

Unfortunately GIT commands like prune and reset or push didn't work for me. Prune worked once and then the issue returned.

The permanent solution which worked for me is to edit a git file manually. Just go to the project's .git folder and then open the file packed-refs in a text editor like Notepad++. Then navigate to the row with the failing branch and update its guid to the expected one.

If you have a message like:

error: cannot lock ref 'refs/remotes/origin/feature/branch_xxx': is at 425ea23facf96f51f412441f41ad488fc098cf23 but expected 383de86fed394ff1a1aeefc4a522d886adcecd79

then in the file find the row with refs/remotes/origin/feature/branch_xxx. The guid there will be the expected (2nd) one - 383de86fed394ff1a1aeefc4a522d886adcecd79. You need to change it to the real (1st) one - 425ea23facf96f51f412441f41ad488fc098cf23.

Repeat for the other failing branches and you'll be good to proceed. Sometimes after re-fetch I had to repeat for the same branches which i already 'fixed' earlier. On re-fetch GIT updates guids and gives you the latest one.

Anyways the issue isn't a show stopper. The branch list gets updated. This is rather a warning.

kenorb
  • 118,428
  • 63
  • 588
  • 624
Andrei S.
  • 41
  • 2
2

Try this, it worked for me. In your terminal: git remote prune origin.

Ranganatha G V
  • 415
  • 5
  • 12
1

git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin git reflog expire --expire=now --all git gc --prune=now

Luciano
  • 31
  • 1
1

I had the same problem which was caused because I resetted to an older commit even though I already pushed to the remote branch.

I solved it by deleting my local branch then checking out the origin branch git checkout origin/my_branch and then executing git checkout my_branch

Black
  • 12,789
  • 26
  • 116
  • 196
0

Same case here, but nothing about comments posted it's right in my case, I have only one branch (master) and only use Unix file system, this error occur randomly when I run git fetch --progress --prune origin and branch is ahead or 'origin/master'. Nobody can commit, only 1 user can do push.

NOTE: I have a submodule in acme repository, and acme have new submodule changes (new commits), I need first do a submodule update with git submodule update.

[2014-07-29 13:58:37] Payload POST received from Bitbucket
[2014-07-29 13:58:37] Exec: cd /var/www/html/acme
---------------------
[2014-07-29 13:58:37] Updating Git code for all branches
[2014-07-29 13:58:37] Exec: /usr/bin/git checkout --force master
[2014-07-29 13:58:37] Your branch is ahead of 'origin/master' by 1 commit.
[2014-07-29 13:58:37]   (use "git push" to publish your local commits)
[2014-07-29 13:58:37] Command returned some errors:
[2014-07-29 13:58:37] Already on 'master'
---------------------
[2014-07-29 13:58:37] Exec: /usr/bin/git fetch --progress --prune origin
[2014-07-29 13:58:39] Command returned some errors:
[2014-07-29 13:58:39] error: Ref refs/remotes/origin/master is at 8213a9906828322a3428f921381bd87f42ec7e2f but expected c8f9c00551dcd0b9386cd9123607843179981c91
[2014-07-29 13:58:39] From bitbucket.org:acme/acme
[2014-07-29 13:58:39]  ! c8f9c00..8213a99  master     -> origin/master  (unable to update local ref)
---------------------
[2014-07-29 13:58:39] Unable to fetch Git data

To solve this problem (in my case) simply run first git push if your branch is ahead of origin.

Tecnocat
  • 1,073
  • 1
  • 11
  • 14
  • 3
    Your answer here only relates to the fact that your local repository is ahead of the origin after a new commit. This is the natural state of a local git commit operation and does not relate to the original question. – davient Aug 04 '15 at 08:55
0

I know this is old, but I have my own fix. Because I'm using source tree, this error happens because someone create a new branch. The source tree is confused about this. After I press "Refresh" button beside the "remote branch to pull" combobox, it seems that sourcetree has updated the branch list, and now I can pull successfully.

Chen Li Yong
  • 3,904
  • 5
  • 39
  • 83
0

I faced same issue , I just deleted the remote branch and created new branch from the master and merged my changes from old feature branch to new feature branch . Now i tried pull and push requests its worked for me

-2

After searching constantly, this is the solution that worked for me which entails unsetting/removing the Upstream

git branch --unset-upstream
Sugoi Reed
  • 357
  • 3
  • 3