'cannot lock ref 'refs/remotes/origin/master'

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>
git


Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

git remote prune origin

This command worked for me.

Solution 4:[4]

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.

Solution 5:[5]

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.

Solution 6:[6]

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.

Solution 7:[7]

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 -

Solution 8:[8]

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.

Solution 9:[9]

its working for me after delete branch

git push origin --delete <branchName>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 THess
Solution 2
Solution 3 Reza Rahemtola
Solution 4 VonC
Solution 5 Death111
Solution 6 General Mac
Solution 7 David Betterton
Solution 8 ap.singh
Solution 9 sweetnandha cse