723

After cloning from remote git repository (at bettercodes) I made some changes, commited and tried to push:

git push origin master

Errors with:

error: cannot lock existing info/refs
fatal: git-http-push failed

This case regards already existing repository.

What I did before, was:

  1. git config --global http.sslVerify false
  2. git init
  3. git remote add [url]
  4. git clone
  5. change data
  6. git commit

At 'bettercodes' I have no access to git log.

I'm using Windows. The detailed error was:

C:\MyWorkStuff\Projects\Ruby\MyProject\>git push origin master
Unable to create branch path https://user:password@git.bettercodes.org/myproject/info/
error: cannot lock existing info/refs
fatal: git-http-push failed

I cloned before, then changed the code and committed.

Guildenstern
  • 2,179
  • 1
  • 17
  • 39
AnnD
  • 7,231
  • 3
  • 15
  • 4
  • Two possible reasons: a) Another instance of git is running (kill all git processes or reboot) b) .git folder was created as Administrator (try administrator command line for the operation) – FractalSpace Oct 28 '16 at 19:42
  • For me, I resolved the error by calling `git fetch` before `git pull`. – Levi Fuller Feb 02 '18 at 20:03
  • In my case the problem was the cases of the directory name. The branch name was "origin/no-ticket", but in my local dir the name was NO-TICKET so I just renamed it and it worked. – Constantino Cronemberger Oct 28 '21 at 11:15

37 Answers37

1424

For me this worked (won't change in remote):

git remote prune origin

Since this answer seems to help a lot of people, I dug a little bit into what actually happens here. What this will do is remove references to remote branches in the folder .git/refs/remotes/origin.

So this will not affect your local branches and it will not change anything remote, but it will update the local references you have to remote branches. It seems in some cases these references can contain data Git cannot handle correctly.

Sebastialonso
  • 1,437
  • 18
  • 34
arno_v
  • 18,410
  • 3
  • 29
  • 34
  • 5
    I've added some background info, but I must honestly say I don't know exactly why and how this works :) – arno_v Jan 12 '18 at 07:36
  • 3
    git remote prune origin work for me. But I have delete all the reference in .git/refs/remotes/origin. – Isuru Madusanka Apr 24 '18 at 09:41
  • 7
    This is exactly what `git` suggests doing, but I was reluctant to do it because the command sounds like it does something to the remote. – Throw Away Account Oct 10 '18 at 18:39
  • 23
    I ran `git gc --prune=now` – Stanley Mohlala Jan 24 '19 at 06:23
  • I was getting similar error for "git fetch" . git remote prune origin fixed it. – qqqqq Sep 10 '19 at 20:21
  • 1
    How is this different to `git remote update origine --prune`? (for git 2.23.0) – Paul Razvan Berg Nov 22 '19 at 15:09
  • 102
    This is the SCARIEST git cmd I have run in a while. (PS: it worked) – Sat Thiru Feb 08 '20 at 18:46
  • Not sure if this makes it any less confusing, but: "git remote prune # update local/remote sync" (https://gist.github.com/cferdinandi/ef665330286fd5d7127d) – Mikael Weiss Oct 23 '20 at 18:42
  • Also https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes – Mikael Weiss Oct 23 '20 at 18:45
  • 2
    If you still don't believe this is harmless, which I didn't, you can run `git remote prune origin --dry-run` first. This answer solved it for me. – CopyJosh Mar 09 '21 at 03:15
  • This same symptom may also be seen during transient race conditions among concurrent `git push` commands. In this case, a simple retry of the `git push` might be sufficient. See related discussion at http://git.661346.n2.nabble.com/Git-push-race-condition-td7606412.html and concurrency background at https://stackoverflow.com/a/19964392/1484823 git locking mechanism to ensure concurrency – Guillaume Berche Jun 18 '21 at 16:14
  • 1
    eh, didn't work for me. I can't create new branches :( – ScottyBlades Sep 22 '21 at 07:02
  • This worked for me, but I could not push a new branch (`git push --set-upstream origin branch_name`) until after I ran `git fetch --all` – trex Nov 24 '21 at 19:03
  • 6
    `git pull --prune` will do the same but with a less scary command. – Nelson Aug 21 '22 at 22:31
  • `git pull --prune` worked for me after `git remote prune origin` somehow did not fully resolve the issue. – jchook Feb 24 '23 at 01:54
  • that doesn't help me, still see `fatal: cannot lock ref`. – nick Jun 08 '23 at 10:07
  • Is there any way we can say git to ignore such errors. In my repo, we have reached to a point where there are 100s of active branches and we get this error every time we try to fetch. Git should really provide a way to handle what to do... and there really should be a way to IGNORE it for one or two branches. – Jay Joshi Jun 28 '23 at 01:59
702

You want to try doing:

git gc --prune=now

See https://www.kernel.org/pub/software/scm/git/docs/git-gc.html

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
kiran.gilvaz
  • 7,137
  • 2
  • 15
  • 11
  • Is --prune=now the same as --prune=all? If so, the documentation warns that you may lose unanchored objects. If there are unanchored objects you should probably try to reconcile them before pruning. – Assaf Israel Dec 02 '16 at 02:51
  • 12
    Life saver, thank you. `git pull` was stuck with the similar error message. – Phil Brubaker May 12 '17 at 19:48
  • 5
    It helped for "git error:cannot lock ref" exception on fetch. Thanks a lot! – Alexander May 17 '17 at 10:20
  • 18
    This worked for me. But then I had to keep executing the same command each time I use a `git` command that deals with remote. `git remote prune origin` resolved the issue once and for all. – Keyur Golani Jun 02 '18 at 01:12
  • fwiw: git gc: _Runs a number of housekeeping tasks within the current repository, such as compressing file revisions... removing unreachable objects which may have been created from prior invocations of git add, packing refs, pruning reflog, rerere metadata or stale working trees..._ **AND** prune: _Prune loose objects older than date (default is 2 weeks ago, overridable by the config variable gc.pruneExpire). --prune=now prunes loose objects regardless of their age and increases the risk of corruption if another process is writing to the repository concurrently... --prune is on by default._ – ruffin Apr 13 '20 at 15:17
  • You made my day. Thanks a lot. But, I have to execute this command every time when I switch the branch. Does any generic solution work for all the branches? That will be a great help. Thanks again. – Sunny Vaghadia Jun 21 '22 at 04:44
  • this git cmd looks scary but it works like charm. Thanks. – Abhijit Patil Jun 22 '22 at 08:59
  • For me, `git remote prune origin` didn't work but `git gc --prune=now` did the trick. – Charlie Reitzel Jan 27 '23 at 17:20
  • that doesn't help me, still see `fatal: cannot lock ref`. – nick Jun 08 '23 at 10:07
  • What to do when I keep running into this issue? It seems whenever I pull latest master, I will run into this problem again next time. – Zhen Liu Jul 17 '23 at 06:19
240

This happened to me when my git remote (bitbucket.org) changed their IP address. The quick fix was to remove and re-add the remote, then everything worked as expected. If you're not familiar with how to remove and re-add a remote in git, here are the steps:

  1. Copy the SSH git URL of your existing remote. You can print it to the terminal using this command:

    git remote -v

which will print out something like this:

 origin git@server-address.org:account-name/repo-name.git (fetch)
 origin git@server-address.org:account-name/repo-name.git (push)
  1. Remove the remote from your local git repo:

    git remote rm origin

  2. Add the remote back to your local repo:

    git remote add origin git@server-address.org:account-name/repo-name.git

Max
  • 1,054
  • 1
  • 12
  • 20
johnnyclem
  • 3,284
  • 1
  • 17
  • 14
  • 10
    I've tried everything else, like git gc, git prune, rm 'file with lock error', git update server info, etc. Only this answer worked for me. Sometimes it's like a windows reboot, reboot and it'll work. Same here, just remove and add the repo again, and everything goes fine ;) – Marquinho Peli Aug 15 '16 at 14:29
  • 20
    After the above procedure, I also needed to tell git to track the remote branch again with e.g.: `git branch -u origin/master` – fotinsky May 10 '17 at 15:33
  • 1
    This blew away all my remote tracking info in .git/config and didn't actually work. – ThomasMcLeod Jun 11 '19 at 21:39
  • This worked for me as well. All the others didn't work. – dondrzzy Oct 10 '19 at 19:22
  • This worked for me as well. All the others like git gc didn't work. – Eduardo de Santana Jul 27 '20 at 17:22
  • 1
    @ThomasMcLeod it blew away my remote tracking too, but I regain those with `git branch --set-upstream-to=origin/ ` one by one – gabore Dec 02 '20 at 09:39
  • 1
    This one definitely worked best for me. The prune stuff only worked for a few next commands, but after that the nasty problem would come back. This solution really did the trick. – Rigo Sarmiento Feb 15 '21 at 05:49
  • WARNING! johnnyclem failed to warn this action will drop all your branch links to origin. You have to add them back afterwards. Thanks to @gabore and fotinsky for adding the information. Note you may *also* need to run `git fetch` first to get the remote branches that you want to add back. WARNING! johnnyclem - it would be great if you could complete your answer. – NeilG Jun 14 '22 at 02:34
  • I think you can reconnect _all_ of the upstreams at once with `git remote update` – StephenWeiss Sep 09 '22 at 17:39
  • This is the most viable solution for me also. `git update-ref -d refs/remotes/origin/path/to/branch` also works but you'd need to apply it to each individual ref that is an issue. – Dark Star1 Apr 28 '23 at 12:13
104

This is what I did to get rid of all the lock ref issues:

git gc --prune=now
git remote prune origin

This is probably what you only need to do too.

More about git gc command here: https://git-scm.com/docs/git-gc

More about git remote prune here: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-empruneem

MiKr13
  • 1,297
  • 13
  • 20
aliibrahim
  • 1,695
  • 1
  • 12
  • 18
80

Running command git update-ref -d refs/heads/origin/branch fixed it.

akansh tayal
  • 949
  • 6
  • 3
  • 9
    That command did the trick for me as well, although my remote branch ref was slightly different: `git update-ref -d refs/remotes/origin/my_branch` – ndeslandes May 31 '19 at 12:32
  • 4
    This worked for me, it appears this was a case sensitivity issue. There were two branches with the same name that were pushed to origin by another git user, one had all lower case and one that was title case. – th3uiguy Oct 07 '19 at 19:22
  • This fixed it... Caused by 2 branches with same name different cases. – Andrew Oct 21 '21 at 13:32
54

What worked for me was:

  1. Remove .git/logs/refs/remotes/origin/branch
  2. Remove .git/refs/remotes/origin/branch
  3. Run git gc --prune=now

More about git gc command here: https://git-scm.com/docs/git-gc

MiKr13
  • 1,297
  • 13
  • 20
emirc
  • 1,948
  • 1
  • 23
  • 38
31

I fixed this by doing the following

git branch --unset-upstream
rm .git/refs/remotes/origin/{branch}
git gc --prune=now
git branch --set-upstream-to=origin/{branch} {branch}
#or git push --set-upstream origin {branch}
git pull

This assuming that your local and remote branches are aligned and you are just getting the refs error as non fatal.

P.M
  • 2,880
  • 3
  • 43
  • 53
FrankMonza
  • 2,024
  • 16
  • 26
29

Before pull you need to clean your local changes. following command help me to solve.

git remote prune origin

and then after

git pull origin develop

Hopes this helps!

Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
  • 1
    it doesn't solve the issue for me. Still get the same error – asa Jan 17 '21 at 09:41
  • 1
    @AfonsoSchulzAlbrecht I needed to do the solution https://stackoverflow.com/a/59645527/4502379 provided by emirc – rogaa Jan 20 '21 at 07:02
27

I had this issue because I was on a branch that had a similar name to an upstream branch. i.e. the upstream branch was called example-branch and my local branch was called example-branch/backend. The solution was changing the name of my local branch like so:

git branch -m <new name goes here>
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Had a similar problem. Had an upstream branch "questlines/layout" and a local branch "questlines" (Using Gitkraken). Just renamed "questlines" to "questlines/bugfix" and it worked as it did before. – Benjamin Basmaci Sep 05 '20 at 14:01
  • This solution was the first that worked for me. Had many similar branch names. Created a new branch with an entirely different name and it worked. git remote prune didn't work for me. Thank you! – Koen Mar 24 '23 at 09:21
23

As a reference for Visual Studio Code (vscode, code) (and possibly other IDEs)

I had to do command: Git: Fetch (Prune)

Command line alternative should be: git fetch --prune

Then restart the whole IDE.

And to give some perspective I will quote BitBucket's docks:

What’s the Difference Between Git Prune,
Git Fetch --prune, and Git Remote Prune?

git remote prune and git fetch --prune do the same thing: delete the refs to branches that don't exist on the remote. This is highly desirable when working in a team workflow in which remote branches are deleted after merge to main. The second command, git fetch --prune will connect to the remote and fetch the latest remote state before pruning. It is essentially a combination of commands:
git fetch --all && git remote prune
The generic git prune command is entirely different. As discussed in the overview section, git prune will delete locally detached commits.

jave.web
  • 13,880
  • 12
  • 91
  • 125
15

This is probably resolved by now. But here is what worked for me.

  1. Location:

    • If locked repository is on the server-side:

      1. ssh to your git repository on the server.
      2. Login as user which has permissions to modify the repository and navigate to the repository on your server.
    • If locked repository is local only:

      1. Open the git console and navigate to the repository directory.
      2. Run this command:

        git update-server-info
        
  2. Fix the permissions on your (remote or/and local) repository if you have to. In my case I had to chmod to 777 and chown to apache:apache

  3. Try to push again from the local repository:

    git push
    
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
WolfTail
  • 159
  • 1
  • 2
8

This is how it works for me.

  1. look up the Apache DAV lock file on your server (e.g. /var/lock/apache2/DAVlock)
  2. delete it
  3. recreate it with write permissions for the webserver
  4. restart the webserver

Even faster alternative:

  1. look up the Apache DAV lock file on your server (e.g. /var/lock/apache2/DAVlock)
  2. Empty the file: cat /dev/null > /var/lock/apache2/DAVlock
  3. restart the webserver
schmunk
  • 4,708
  • 1
  • 27
  • 50
  • This was my issue. Thanks for the post. I ran the remove and permissions all in one shot. `#> rm DAVLock; touch DAVLock; chown www-data.www-data DAVLock; chmod 755 DAVLock; service apache2 restart` – djneely Jan 21 '14 at 15:12
8

This sounds like a permissions issue - is it possible you had two windows open, executing with separate rights? Perhaps check ownership of the .git folder.

Perhaps check to see if there is an outstanding file lock open, maybe use lsof to check, or the equivalent for your OS.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Josh
  • 6,155
  • 2
  • 22
  • 26
5

In my case a branch was moved to a subdirectory and the directory was called as the branch. Git was confused by that. When I deleted the local branch (in SourceTree just with right click delete) everything worked as usual.

CodingYourLife
  • 7,172
  • 5
  • 55
  • 69
4

In my case after getting this message I did the checkout command and was given this message:

Your branch is based on 'origin/myBranch', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

After running this command I was back to normal.

Colin
  • 1,758
  • 1
  • 19
  • 24
4

Run git fetch --all before git pull. That should solve the problem.

ttfreeman
  • 5,076
  • 4
  • 26
  • 33
4

I had a typical Mac related issue that I could not resolve with the other suggested answers.

Mac's default file system setting is that it is case insensitive.

In my case, a colleague obviously forgot to create a uppercase letter for a branch i.e.

testBranch/ID-1 vs. testbranch/ID-2

for the Mac file system (yeah, it can be configured differently) these two branches are the same and in this case, you only get one of the two folders. And for the remaining folder you get an error.

In my case, removing the sub-folder in question in .git/logs/ref/remotes/origin resolved the problem, as the branch in question has already been merged back.

Denis
  • 165
  • 1
  • 1
  • 12
  • I had the same problem on Windows. Someone created a second branch with the same name but different case. I had to delete one of the two branches on the git server for it to work. – ps2goat Dec 30 '20 at 00:21
  • Using mac too and removing the folder didn't fix the issue, changing the upper case in folder name (branch name before the / in the refs/remotes folder) to lower has resolved the issue for me – Hasnaa Ibraheem Apr 28 '22 at 06:53
4

Update:

You might need to edit your ~/.netrc file:

https://bugs.launchpad.net/ubuntu/+source/git-core/+bug/293553


Original answer:

Why did you disable ssl? I think this might have to do with you not being able to push via https. I'd set it back and try to push again:

git config –global http.sslVerify true
Liam
  • 27,717
  • 28
  • 128
  • 190
rtn
  • 127,556
  • 20
  • 111
  • 121
3

Case 1: Let check branches from git-server if they are duplicated or not.

Example: two branches below are duplicated:

    - upper_with_lower
    - UPPER_with_lower

---> Let consider removing one of them.

Case 2: The branch you are pushing are duplicated with other branch.

alphaplus
  • 344
  • 1
  • 3
  • 9
2

I had this problem, when I was trying to create a new feature branch that contained name of the old branch, e.g. origin - branch1 and I wanted to create branch1-feature. It wasn't possibble, but branch1/feature was already.

2

Aside from the many answers already supplied to this question, a simple check on the local repo branches that exist in your machine and those that don't in the remote, will help. In which case you don't use the

git prune

command as many have suggested.

Simply delete the local branch

git branch -d <branch name without a remote tracking branch by the same name>

as shown in the attached screenshot using -D to force delete when you are sure that a local branch does not have a remote branch tracked.

lock ref error git

user2347763
  • 469
  • 2
  • 10
2

Check that you (git process actually) have access to file .git/info/refs and this file isn't locked by another process.

Ivan Danilov
  • 14,287
  • 6
  • 48
  • 66
2

For me it was happening when I try to create a new branch with a name like this one:

master/pre-upgrade

changing it to another name like:

pre-upgrade/master

did the trick!

Some additional info on this issue

Git stores the branch information as files in a folder structure where the last string after the / is a file that contains the commit SHA# of the head of the branch.

This means that a branch called

a/b/c

will be stored in

.git/refs/heads/a/b/c

If you would try to add a branch named

a/b/c/d

git will try to store that in

.git/refs/heads/a/b/c/d 

but that wont be possible because b is a file and cant store other files in it.

I.E consider git branch names as paths because they are used as paths by git.

Buddy
  • 99
  • 1
  • 2
  • 11
Omar Dulaimi
  • 846
  • 10
  • 30
2

I had the same error message, root cause was a rewrite of the history (branch renaming).

That worked for me:

git remote prune origin

Source: https://codedaily.in/git-error-cannot-lock-refs/

Marc Wäckerlin
  • 662
  • 6
  • 7
2

I got this issue when I tried to create a branch that starts with a current branch. I had a branch named develop and I tried to create a branch called develop/myFeature.

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
1

In my case, it was connected with the branch name that I had already created.

To fix the issue I've created a branch with the name that for certain shouldn't exist, like:

git checkout -b some_unknown_branch

Then I've cleared all my other branches(not active) because they were just unnecessary garbage.

git branch | grep -v \* | grep -v master | xargs git branch -D

and then renamed my current branch with the name that I've intended, like:

git checkout -m my_desired_branch_name
Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
1

In my case I had to manually delete old tags which had been removed on remote.

joliejuly
  • 2,127
  • 1
  • 21
  • 24
1

If you are in the fortunate position of having no local work to commit/push and time to get coffee, you could simply delete your local copy of the repo and re-clone

andrew pate
  • 3,833
  • 36
  • 28
1

I had a similar issue that totally confused me. I had a local branch named test and was trying to fetch and checkout remote branch named test/some-changes.

Fix was just to remove a stale test branch, and I could fetch and checkout the remote one.

equi
  • 729
  • 8
  • 19
1

It can also happen that if you are naming branch using group words like feat, bugfix, e.g., feat/awesome-feature and there already exists a branch at origin with name feat, i.e., origin/feat, your push will be rejected with error "refs/heads/feat exists; cannot create refs/heads/feat/awesome-feature".

Devansh Maurya
  • 832
  • 12
  • 11
1

None of the answers worked for me... so I recloned the repo, which re-created the correct refs. Last resort option, but a fast and efficient fix

Yann VR
  • 486
  • 5
  • 9
0

I saw this error when trying to run git filter-branch to detach many subdirectories into a new, separate repository (as in this answer).

I tried all of the above solutions and none of them worked. Eventually, I decided I didn't need to preserve my tags all that badly in the new branch and just ran:

git remote remove origin
git tag | xargs git tag -d
git gc --prune=now
git filter-branch --index-filter 'git rm --cached -qr --ignore-unmatch -- . && git reset -q $GIT_COMMIT -- apps/AAA/ libs/xxx' --prune-empty -- --all
tessafyi
  • 2,273
  • 2
  • 19
  • 28
0

This error was coming on doing

git fetch

but in my case I just wanted the update of the main branch which can be done using

git fetch origin main

Not a solution to lock ref problem but it helped me avoid the problem :P

Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
0

I fixed this problem by simply using git push origin -u branchName

David Ouma
  • 41
  • 3
0

None of these worked for me. Some solutions would work for the first git pull afterwards, but the error would re-appear for every pull after. So here's what I did.

I edited the .git/packed-refs in my repo, and removed the line for the ref/repo that git didn't like. Then I did a fetch+pull again, and the error seems to be gone.

SeanMC
  • 1,960
  • 1
  • 22
  • 33
0

In case of bettercodes.org, the solution is more poetic - the only problem may be in rights assigned to the project members. Simple members don't have write rights! Please make sure that you have the Moderator or Administrator rights. This needs to be set at bettercodes.org at the project settings by an Administrator, of course.

yman
  • 349
  • 4
  • 18
-1

for me, removing .git/info/ref kick things going.There shouldn't be a ref file when git is not running. But in my case there were one for whatever reason and caused the problem.

Removing the file will not remove any of your local commits or branches.

hzh
  • 342
  • 2
  • 15