69

I always got cannot lock ref 'refs/remotes/origin/master' when I execute git pull first time if there are some updates.

The full console log is belows.

D:\code\react-native\expo-multi-screen-starter>git pull
error: cannot lock ref 'refs/remotes/origin/master': is at b2459b4d5af42622cba55f9fe47ccd14fbd879bc but expected 76f11048c866cfe3e6570eaacf90db3cb7732723
From github.com:liudonghua123/expo-multi-screen-starter
 ! 76f1104..b2459b4  master     -> origin/master  (unable to update local ref)

D:\code\react-native\expo-multi-screen-starter>git pull
 App.js                                          | 19 ++++-----
 src/navigation/AuthStack.js                     | 14 +++++++
 src/navigation/RootNavigator.js                 | 22 ++++++++++
 src/navigation/TabNavigator.js                  |  4 +-
 src/screens/AuthLoadingScreen.js                | 35 ++++++++++++++++
 src/screens/HomeScreen.js                       | 19 +++++++++
 src/screens/{LoginScreen.js => SignInScreen.js} | 54 ++++++++++++++++++++++---
 7 files changed, 147 insertions(+), 20 deletions(-)
 create mode 100644 src/navigation/AuthStack.js
 create mode 100644 src/navigation/RootNavigator.js
 create mode 100644 src/screens/AuthLoadingScreen.js
 rename src/screens/{LoginScreen.js => SignInScreen.js} (61%)

D:\code\react-native\expo-multi-screen-starter>
2
  • 1
    This sort of error message usually implies some permissions issue with your repository. What those might be on Windows, I'm not sure.
    – torek
    Commented Sep 27, 2019 at 2:51
  • This answer explains some of the underlying Git management for the reverse case, where there's a blocker on the local refs that prevent the remote being created, but some of the explanation may still be relevant: stackoverflow.com/a/76012255
    – NeilG
    Commented Apr 14, 2023 at 7:56

13 Answers 13

140

You need to update the reference using following Git command on Git bash:

$ 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.

4
  • 5
    A permanent solution that finally worked!
    – jh95
    Commented Feb 3, 2021 at 16:16
  • 2
    This solution should have a star mark. It worked for me! Commented Oct 6, 2021 at 6:11
  • "git remote prune origin" didn't work for me, but this command did.
    – Patric
    Commented Aug 29, 2023 at 8:01
  • In my case, the reason for "cannot lock ref" was that somebody had two branches with names differing only in case, branch and brANCH. For the remote Git it was OK, but my local clone on a Windows machine got confused. The update-ref command helped. Commented Jul 31 at 6:57
46

My error looked a little different:

error: cannot lock ref 'refs/remotes/origin/releases/branch1': 'refs/remotes/origin/ releases' exists; cannot create 'refs/remotes/origin/releases/branch1'

! [new branch] releases/branch1 -> origin/releases/branch1 (failed to update the local link).

The error disappeared after executing the command:

git remote prune origin

By the way, TortoiseGit advised her to do it. But this

git update-ref -d refs/remotes/origin/releases/branch1

command did not help.

0
19
git remote prune origin

This command worked for me.

18

I was able to fix it by just deleting .git/refs/remotes/ and it was restored automatically.

3
  • 1
    This answer worked for me. Before this I tried other answers but they didn't work. --> git remote prune origin --> git update-ref -d refs/remotes/origin/releases/branch1
    – venugopal
    Commented Oct 21, 2022 at 0:07
  • Previously "git remote prune origin" worked for me. But somehow recently it stopped working for me. Deleting .git/refs/remotes/ worked for me this time. Thanks. Commented May 15, 2023 at 14:38
  • I had a blue screen on the machine running my GitLab runner and tried everything under the sun - this was the only thing that worked.
    – Jammer
    Commented Jun 30 at 19:27
5

If you have a VSCode running, you might want to close it first.
As seen in "microsoft/vscode issue 47141"

This issue occurs when git-radar is doing a fetch in background and user runs git fetch/pull.
Git has index.lock to lock index during a fetch, so you cannot corrupt the index by doing concurrent fetches.
Your git command fails because of this lock prevents git modifying index.

You can also try git remote prune origin as mentioned here.

2
  • 2
    Yes, I run git pull in the integrated terminal in VSCode often. git remote prune origin seems to remove references to remote branches in the folder .git/refs/remotes/origin, Is it safe operation, no influence to the git pull command later? Commented Sep 27, 2019 at 5:37
  • 1
    @DonghuaLiu Yes, the remote branches will be back at the next pull, which includes a fetch.
    – VonC
    Commented Sep 27, 2019 at 7:38
3

git remote prune origin

it worked fine for me. I think that what the command is doing is comparing the list of branches between your local and remote repositories and remove the one that doen't exist anymore in the remote repository from the local one.

1
  • The only command that works for me
    – Fatima
    Commented May 17, 2022 at 4:25
1

In my case, the branch I was dealing with was transient. So I ended up deleting it from the remote.

git push origin --delete <branchName>

Also in my case, I wasn't authorized to delete a remote branch in our repository. So before I could run the above command I needed that permission.

1

its working for me after delete branch

git push origin --delete <branchName>
0

I had this issue and worked out a possible root cause, there were multiple branches with the same name, differing only in case, eg

db/AA-12345-my-branch and db/AA-12345-MY-BRANCH

The answer from Sanjay Gupta - specifically the git update-ref resolved the local issue -

0

I had this issue and spend a lot of time fixing this. But Nothing worked for me, I just created a new branch from the same branch and pushed the code to the new branch.

0

I had this error but trying to upgrade flutter.

ProcessException: Process exited abnormally: error: cannot lock ref 'refs/remotes/origin/stable': is at 52

If this is your case don't worry, just check that you are in the correct channel (with flutter channel) and try again the flutter upgrade

0

checkout to a new branch

git checkout -b new-branch

Push the current remote branch

git push [origin] new-branch
0

FYI The only thing that worked for me was to do:

git cache clean

I tried all of the above and that did not work for my particular issue.

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.
    – Community Bot
    Commented Sep 30, 2023 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.