27

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,198
  • 2
  • 12
  • 25
  • 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

9 Answers9

55

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
  • 993
  • 1
  • 10
  • 21
Sanjay Gupta
  • 651
  • 4
  • 3
15

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
  • 151
  • 1
  • 3
6
git remote prune origin

This command worked for me.

Reza Rahemtola
  • 1,192
  • 7
  • 15
  • 29
Sachi
  • 61
  • 1
  • 2
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.

VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
  • 1
    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
1

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
  • 11
  • 1
  • 2
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,266
  • 1
  • 10
  • 10
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,140
  • 4
  • 13
  • 34
0

its working for me after delete branch

git push origin --delete <branchName>
sweetnandha cse
  • 349
  • 4
  • 7