350

I only have a master branch and I'm getting this error every time I try to git pull:

error: Couldn't set refs/remotes/origin/master
From /var/lib/git/xxx/project
 ! a0f80ea..49177a3  master     -> origin/master  (unable to update local ref)

And when I run git pull origin master I get:

error: Couldn't set ORIG_HEAD
fatal: Cannot update the ref 'ORIG_HEAD'.

I have been searching but can't find why.

swimfar2
  • 103
  • 8
user115561
  • 3,631
  • 2
  • 14
  • 8
  • 2
    Where is the local repository? Did you create it as a different user than the one you are using to execute the pull? It sounds like a file permission problem. – tpg2114 Apr 09 '12 at 03:47
  • Yeah you are right after you said the owner of the project files was another user, now my question seems so fool, but you gave me the answer, please make it an answer to choose it as the best ;) – user115561 Apr 09 '12 at 05:22
  • please @tpg2114 add this as an answer to chose it – user115561 Apr 10 '12 at 18:55

30 Answers30

622

My team and I ran into this error, unable to update local ref, when doing a pull in SourceTree.

Update 2020: Per @Edward Yang's answer below, @bryan's comment on this answer, and this question/answer you may need to run both git gc --prune=now and git remote prune origin. Running only the former has always worked for me but based on ppl's responses I think both are necessary to address different causes of the error.

We used:

git gc --prune=now

This removes any duplicate reference objects which should fix the issue.

Here are a few links where you can learn more about git references and pruning :

git tip of the week

git-prune documentation

git references

Greg Venech
  • 8,062
  • 2
  • 19
  • 29
  • 2
    Worked for me too, same message, Sourcetree on Windows 7 – James Westgate Jan 13 '15 at 15:50
  • 27
    May need both these commands:`git gc --prune=now` `git remote prune origin` from https://stackoverflow.com/questions/2998832/git-pull-fails-unable-to-resolve-reference-unable-to-update-local-ref – bryan Jan 15 '20 at 21:50
  • 1
    I tried `git remote prune origin` and it didn't work for me. But after that tried this `git gc --prune=now`, and it worked! Not sure, if both were needed in that order, or only this one. – Anurag Jan 29 '20 at 10:05
  • 2
    @Skipjack It does solve the issue temporarily. But again when I try to fetch and switch to a new branch, I'm getting the same error. – Balasubramani M Sep 17 '20 at 09:50
  • worth noting that "git remote prune origin" the "origin" is whatever your git is complaining about --- "refs/remotes/origin/master" <--- whatever origin is in that string. – zBeeble Jun 11 '21 at 20:53
  • This worked for me when I switched from bash to zsh. I was getting the error `cannot update the ref 'refs/heads/mybranch': unable to append to '.git/logs/refs/heads/mybranch': Permission denied` – antonkronaj Mar 03 '22 at 12:57
  • 1
    Worked for me --> error: fatal: cannot update the ref '': unable to append to '': Permission denied; Solution : git gc --prune=now – Brijesh Ray Jul 21 '22 at 14:38
  • Did not work for me. No change in error after running both commands. – R-D Sep 21 '22 at 15:38
324

I solved as below:

git remote prune origin

Edward Yang
  • 3,340
  • 1
  • 11
  • 5
64

with gitbach line commande, use git update-ref to update reference of your local branch:

$ git update-ref -d refs/remotes/origin/[locked branch name]

then pull using $ git pull

[locked branch name] is the name of the branch that the error is happening because of mismatch of commit Ids.

THess
  • 1,003
  • 1
  • 13
  • 21
T.Moez
  • 741
  • 5
  • 6
29

Problem

Windows users can often have this problem

git pull 

gives the error: error: cannot lock ref unable to update local ref

Cause

Cause a) There are multiple branches, whose names from the beginning up to any slash (or to the end), differ only in upper and lower case.

Branch name clashing (upper/lower case)
#######################################

# Example 1)
#############################
feature/releasecandidate/fix123
feature/releaseCandidate/improveFeature789
------------------------
               ^
  Identical from beginning up to a slash (here the 2nd one)
  except for the marked letter, where the upper/lower case differs




# Example 2)
#############################
releaseBranch
releasebranch
-------------
       ^
  Identical from beginning to the end
  except for the marked letter

Cause b) Also a problem on linux: One branch is a prefix of another, with a slash boundary:

Prefix with slash-boundary
#######################################

# Example 1) - also a problem on linux
#############################
feature/release2021
feature/release2021/fixIssue07
                   ^
              slash boundary

# Example 2)
#############################
feature/release2022
feature/Release2022/fixIssue99
        ^          ^
  differing case   slash boundary
 (problem on 
   windows)

Solution

Remove the cause (see exact Cause above).

# inspect your branches, to see if you have the upper/lower case problem
git ls-remote --heads YOUR-GIT-URL

For example: create a branch-naming policy, e.g. all in lower-case letters; or letters before the last slash in lower-case. Or some smart hook, that detect a violation. (but note: In cause a) the problem is only on windows, not on linux).

Background

The problem is that windows stores these branches (from example 1 and 2) in the .git folder

# inspect the files/folders under
.git/refs/remotes/origin/

and in Cause a) windows cannot distinguish the differences in upper/lower case, so git on window goes crazy.

In Cause b) you cannot have a folder (e.g. feature/release2021/) with the same name as a file (feature/release2021).

Workaround

A short-term workaround that often works (until you've removed the cause) is:

git pack-refs --all

# delete the contents of .git/refs/remotes/origin/*  
rm -rf .git/refs/remotes/origin/*

git pull; git pull; git pull   # all good? yes!
  • Thanks! This was the clue that finally solved it for me. The repository I was working on is several years old and two years ago someone made a branch called 'test'. Last week another person made a branch called 'Test/[new branch]'. Windows could not create a directory named 'Test' because the branch 'test' already exists. I renamed the 'test' branch to Test/[something useful] and the problem was solved. – Nathilion Jul 09 '21 at 11:27
  • thank you ,it is happening only in windows for me – GvSharma Jul 14 '21 at 04:56
  • Doing some "rm -rf" command for an error mostly occuring on Windows, feels somewhat... – MBODM Sep 30 '22 at 08:37
  • 1
    Excellent comment, this was my actual problem. People naming branches in a way that git on windows hated. I removed those branches (they were test branches) and everything went back to normal. – Harv Nov 16 '22 at 15:43
25

Try to use this command in your git repository root folder:

rm .git/logs/refs/remotes/origin/master 
stasiaks
  • 1,268
  • 2
  • 14
  • 31
Babak
  • 359
  • 3
  • 4
21

rm .git/refs/remotes/origin/master

It works to me!

Xin
  • 33,823
  • 14
  • 84
  • 85
21

DIRECT ANSWER

git remote prune origin
rm .git/refs/remotes/origin/master
git fetch
git pull origin master

Run the above command step by step

Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37
17

I discoverd the same Error message trying to pull from a Bitbuck Repo into my lokal copy. There is also only one Branche Master and the command git pull origin master lead to this Error Message

From https://bitbucket.org/xxx
 * branch            master     -> FETCH_HEAD
error: Couldn't set ORIG_HEAD
fatal: Cannot update the ref 'ORIG_HEAD'.

Solution as follows

  1. git reflog find the number of the last commit
  2. git reset --hard <numnber> reset to the last commit
  3. git pull origin master pull again without error
outofBounds
  • 594
  • 6
  • 18
14

This was enough for Windows:

git pack-refs --all

emert117
  • 1,268
  • 2
  • 20
  • 38
  • 1
    it works, but why? – WinterSoldier Feb 07 '22 at 09:06
  • Yeah would like to know why as well – Pants Jun 08 '22 at 13:13
  • Based on the docs https://git-scm.com/docs/git-pack-refs situations with too many refs can arise: "This command is used to solve the storage and performance problem by storing the refs in a single file, $GIT_DIR/packed-refs. When a ref is missing from the traditional $GIT_DIR/refs directory hierarchy, it is looked up in this file and used if found. Subsequent updates to branches always create new files under $GIT_DIR/refs directory hierarchy. A recommended practice to deal with a repository with too many refs is to pack its refs with --all once, and occasionally run git pack-refs." – SpurguX Mar 17 '23 at 10:49
12

This works perfectly for me:

rm -rf .git/packed-refs .git/rr-cache
cmthakur
  • 2,266
  • 4
  • 18
  • 23
10

Ensure the user that is executing the git pull is the same user that created the repository. The file permissions are incorrect.

tpg2114
  • 14,112
  • 6
  • 42
  • 57
  • For me I had to change the owner of the files in the repo I was trying to git pull in to the correct user with chown. I think this is basically what you were saying although it wasn't obvious to me when I read this. – Ree Apr 12 '17 at 09:49
  • I agree with this, check that owner and group are set to user willing to pull in ".git" repo ( happens if you pulled a branch being "root") a "sudo chown -R " did the job in my case. – jo_ Oct 30 '17 at 10:25
8

What happened over here? The local references to your remote branches were changed and hence when you run git pull, git doesn't find any corresponding remote branches and hence it fails.

git remote prune origin

actually cleans this local references and then run git pull again.

Suggestion - Please run with --dry-run option for safety

therealprashant
  • 701
  • 15
  • 27
6

What worked for me was:

git config --global fetch.prune true

Now it keeps on running prune automatically.

AndrewA
  • 61
  • 1
  • 3
5

I've remove the local ref of the branch with:

git branch -d -r REPO/.git/refs/remotes/origin/BRANCHNAME

then I can do my fetch.

Or more radically

rm -rf REPO/.git/refs/remotes/origin

This works everytime too.

rosa
  • 87
  • 1
  • 3
  • 1
    This works, i have tried all other stuffs, prune them etc... and go back to the simple step :( – Tuan Jinn Jun 28 '21 at 14:59
  • I've eventually realized that this (`git branch -d`, didn't need the -r) was the fix for me. Thank you! – Liran H Jun 18 '23 at 09:09
4

Clone the repository again, and copy the .git folder in your broken project.

3

This is probably a very niche situation, but: I run Windows in a Parallels VM on my MacBook Pro, with my local repos stored on the VM's disk, which is shared with macOS.

If I have a file open in a Mac app from a repo that's located on the Windows VM, I sometimes get the "unable to update local ref" error. The solution when this happens is to simply close the file or quit the Mac app.

daGUY
  • 27,055
  • 29
  • 75
  • 119
  • 1
    This is exactly what my situation was. I had the files open in vs-code and trying to commit using visual studio in the Parallels VM. Closing VSCode and then doing a git pull worked perfectly. Thanks for sharing. – Brad Oct 07 '20 at 18:48
3

Remove file .git/logs/refs/remotes/origin/[Locked Branch Name]

  • 1
    I doubt that this helps - or even works at all. To covince me otherwise please add an explanation of how this should work and why it is supposed to help with the problem. Highlighting the differences to seemingly similar existing other answer would be a bonus. Especially the upvoted one by Babak. – Yunnosch Jul 14 '20 at 17:47
  • @Yunnosch I tried all other answers, but this is the one that helped in the end. – Mijo Jan 13 '23 at 15:08
3

I had the same error, I was pulling from git desktop app. So I tried updating from a DOS command window, and got the same issue.

Then I tried the solution " git gc --prune=now " . Resolution for the issue faced on Git Desktop

git gc --prune=now :This has solved the above issue I was facing. Git Issue

Thanks.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 06 '22 at 14:08
2

This happened to me on OSX where I use a case insensitive file system. Somehow another developer pushed a branch with the same name but different case: My-Branch vs my-branch.

I already had My-Branch checked out and got the error "unable to update local ref" when I did a pull probably because the file system thinks My-Branch == my-branch.

Since we use Github I could solve the problem by deleting one of the branches via Github's GUI.

gabrielf
  • 2,210
  • 1
  • 19
  • 11
  • In my case, both the conflicting branches belonged to another user so I couldn't delete one. Instead, I deleted the branch ref file under .git\refs\remotes, and that fixed it (temporarily - the issue will come back each time I pull until the other user deletes one of his branches). – Jana Mandic Jun 02 '16 at 17:08
  • My issue was also due to local branch and remote having different capitalization. Deleting local branch, using the prune command above, and then checking out from origin solved it – descript Jun 01 '20 at 18:18
2

git pull origin <branch_name_you_are_currently_at> will pull just that branch. It may or may not give you merge conflicts. Resolve them and commit them. It worked for me.

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 21 '21 at 17:07
1

This error with (unable to update local ref) can also happen if you have changed passwords recently and there's some fancy stuff integrating your Windows and Linux logins.

Hazok
  • 5,373
  • 4
  • 38
  • 48
1

Speaking from a PC user - Reboot.

Honestly, it worked for me. I've solved two strange git issues I thought were corruptions this way.

GONeale
  • 26,302
  • 21
  • 106
  • 149
1

I had the same error, I was updating from within Eclipse and I got many errors. So I tried updating from a DOS command window, and got the same issue.

Then I tried the solution " git gc --prune=now " This gave messages that the files were locked in the refs directory.

Eclipse must have had a locked on something in the "refs" directory.
The solution I found was to simply close Eclipse. Then I updated the repository from DOS with a " git PULL " command, and everything worked fine.

Peter Lenahan
  • 61
  • 1
  • 6
1

I fixed this by deleting the locked branch file. It may seem crude, and I have no idea why it worked, but it fixed my issue (i.e. the same error you are getting)

Deleted: .git/refs/remotes/origin/[locked branch name]

Then I simply ran

git fetch

and the git file restored itself, fully repaired

William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33
Isaac S
  • 23
  • 1
  • 6
1

I was also seeing (unable to update local ref) but the issue has been resolved after using following git command:

git gc --prune=now

enter image description here

Bayram Binbir
  • 1,531
  • 11
  • 11
0

I had the same issue on my debian server as the disk is full. No temp file could be created as no space left on device. After cleaning some files, it worked out fine.

fibonacci
  • 2,254
  • 2
  • 17
  • 13
0

This work for me

rm .git/logs/refs/remotes/origin/master 
  • I doubt that this helps - or even works at all. To covince me otherwise please add an explanation of how this should work and why it is supposed to help with the problem. Highlighting the differences to seemingly similar existing other answer would be a bonus. Especially the upvoted one by Babak. – Yunnosch Jul 14 '20 at 17:47
0

One command solution [for Linux/bash users]

  1. Changing permission by changing ownership of the file.

sudo chown username -R .git

(replace 'username' with your username)

Use sudo if owner of the file is sudo.

  1. Deleting the file will also solve the problem.

sudo rm .git/logs/refs/remotes/origin/master

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0

Open Git Bash as admin cd to folder, Git add . Git commit -m "your message" Git push he will now either use built in credentials or ask for username/pw, If built in credentials or username/pw don't work create a PAT and close the window where they ask for credentials a screen will pop up that asks for a PAT

Pieter
  • 111
  • 12
0

This one will probably solve your problem, you get a broken e.g.:

unable to resolve reference 'refs/remotes/origin/develop': reference broken

you have to remove the broken reference and the use the git fetch

git fetch 

and now you can simple use git pull

git pull

reference link: https://codeahoy.com/q/2/Git-pull-fails-Unable-to-resolve-reference-refs-remotes-origin-master-reference-broken

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129