55

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>
Donghua Liu
  • 1,776
  • 2
  • 21
  • 30
  • 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 Sep 27 '19 at 02: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: https://stackoverflow.com/a/76012255 – NeilG Apr 14 '23 at 07:56

12 Answers12

109

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.

THess
  • 1,003
  • 1
  • 13
  • 21
Sanjay Gupta
  • 1,206
  • 1
  • 6
  • 3
35

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.

gorec323
  • 351
  • 3
  • 3
16
git remote prune origin

This command worked for me.

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
Sachi
  • 161
  • 1
  • 3
10

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

SWdV
  • 1,715
  • 1
  • 15
  • 36
  • 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 Oct 21 '22 at 00: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. – Alok Ranjan May 15 '23 at 14:38
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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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? – Donghua Liu Sep 27 '19 at 05:37
  • 1
    @DonghuaLiu Yes, the remote branches will be back at the next pull, which includes a fetch. – VonC Sep 27 '19 at 07: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.

Death111
  • 33
  • 1
  • 5
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.

General Mac
  • 1,846
  • 1
  • 11
  • 15
1

its working for me after delete branch

git push origin --delete <branchName>
sweetnandha cse
  • 705
  • 7
  • 16
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.

ap.singh
  • 1,150
  • 4
  • 15
  • 35
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