1020

I am still getting this error message, when I try to move my project tree on to git repo.

I checked the permissions of my directory with this project and these are set on 777. In terminal in the directory with my_project I set:

git init

and then if I try

git add .

or

git commit -m "first upload"

so I'll get the error

fatal: Unable to create '/path/my_proj/.git/index.lock': File exists.

If no other git process is currently running, this probably means a git process crashed in this repository earlier. 
Make sure no other git process is running and remove the file manually to continue.

I tried also create a new repo and there to commit it, but unfortunately still the same error message.

What is the cause of problem?

dejanualex
  • 3,872
  • 6
  • 22
  • 37
user984621
  • 46,344
  • 73
  • 224
  • 412
  • 1
    I had this issue because I logged in with su in a different terminal, switching back to the first everything went fine – maazza Dec 26 '13 at 15:29
  • http://stackoverflow.com/questions/1367258/using-git-without-sudo-in-many-accounts/33633953#33633953 – Won Jun Bae Nov 10 '15 at 15:48
  • 3
    bro i had the same problem , use "sudo git add . or git commit -m "first upload" " instead of " git add . or git commit -m "first upload" " – marsahllDTitch Mar 27 '16 at 14:05
  • I got it fixed by changing permissions on .git file per http://stackoverflow.com/questions/14127255/remove-git-index-lock-permission-denied , refer to answer from Mahshid Zeinaly – vikramvi May 16 '17 at 15:57
  • Similar-ish... if you can't create the index.lock, that means you have a full disk. If running thru WSL, empty some space on your windows harddrive and then run `wsl --shutdown` and try again. – phyatt Jan 30 '23 at 22:24

33 Answers33

2158

Try

rm -f ./.git/index.lock

In your repository directory. The error message is rather explicit as to what causes it typically, so if you have no other git processes running (which is the normal case), go ahead and delete that file.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Raphael R.
  • 23,524
  • 1
  • 22
  • 18
  • Thanks, that helped me. Now I run `git commit -a` and then I checked out `git status`, there was a message `# On branch master nothing to commit (working directory clean)`. But I still don't see an imported files in my repo on bitbucket. In how time will be transfered the files from my local directory to web repo? – user984621 Oct 22 '11 at 16:35
  • 7
    It doesn't happen automatically. You need to execute the command `git push` to push your changes to the web server. Details on how this works are here: http://gitready.com/beginner/2009/01/21/pushing-and-pulling.html – Raphael R. Oct 22 '11 at 17:03
  • 13
    I'm having the exact same problem but I can't solve it this way because the .git/index.lock file doesn't appear to exist: `touch .get/index.lock` returns `touch: .get/index.lock: No such file or directory` – emersonthis Jan 30 '13 at 17:08
  • On closer inspection, I see a slight difference in the error message. Mine says `:Permission denied` – emersonthis Jan 30 '13 at 17:58
  • 40
    Emerson, you seem to be looking for .get instead of .git which would not exist. – Apie Jul 22 '13 at 12:34
  • I had trouble with this, I used a similar command, with the '-f' from my home directory, I entered: "rm .git/index.lock" – StuckOverflow Oct 17 '13 at 03:19
  • i am running this command but still getting the same error /.git/index.lock': Permission denied Any clue? what else possibly needs to be done ? – vikas Jan 08 '14 at 17:53
  • maybe i have a different git release, needed this: `rm -Force ./.git/index.lock` – RASMiranda Feb 04 '14 at 13:06
  • 3
    Permission denied means you need to give the user you are using Git with read/write permissions on the file. Check out chmod command (on *nix or Properties -> Security tab on WIndows) – Stijn de Witt Feb 04 '14 at 13:25
  • just fyi, in windows I did this: – ssaltman Mar 10 '14 at 15:30
  • 9
    If your error says "Permission Denied", you have probably copied the files from somewhere else, and don't have permissions over the .git directory. Use `ls -l` to see permissions, then use `sudo chown -R username ./*`and `sudo chgrp -R username ./*` to change user and group to your own for all files in the project. – chech Jan 02 '15 at 09:46
  • 1
    Years after this question was answered... your still saving peoples lives! – pourmesomecode Jun 29 '15 at 11:13
  • 1
    In my case, that file doesn't exist either, the error message comes in the middle of a rebase. So there is no file I could remove and this seems like a "real" error. – cslotty Oct 23 '15 at 09:04
  • 4
    i don't have .lock file under .git directory but still I am getting an error. – Paresh Dec 20 '15 at 05:49
  • No one actually reads the messages. It goes more like _Oh, it failed. What do I do?_ haha – IIllIIll Jan 06 '16 at 17:45
  • @Apie `.get` is the Northern version. To be used solely when drinking [Newkie Brown](https://en.wikipedia.org/wiki/Newcastle_Brown_Ale). – Rob Grant Feb 03 '16 at 12:30
  • In my case, it was because I had a script to do some git-related stuff, and my editor was detecting a change of branch, and trying to reload changed buffers while the rest of my script was running. – nroose Nov 14 '16 at 21:21
  • @cslotty This file didn't seem to exist on my system either, but somehow running this command fixed it. – Christopher Bottoms Jun 13 '17 at 22:14
  • The error I am seeing is the same "fatal: Unable to create", Would that not be a permission issue in the .git folder? I don't have an index.lock file but still get the error. – Adam Patterson Jun 14 '17 at 21:49
  • you're my hero! – Bobby Tables Oct 26 '17 at 13:05
  • I had BitDefender which was blocking Git to make changes. I simply allowed Git to run from my BitDefener menu. – SuperHaker Feb 05 '18 at 16:26
  • Its functional now after: Manually deleting the files from ./git/ directory. so no restrictions in deleting the file from console or view. – Farid Abbas Jan 15 '20 at 06:44
  • 'rm' is not recognized as an internal or external command, – Himang Sep 05 '22 at 05:08
  • For those like me that keep coming back to this issue; You can add this line as a shortcut for your (shell) terminal: `alias rmlockfile='rm -f ./.git/index.lock'`. Add it to your .zshconfig file – Loosie94 Mar 21 '23 at 16:07
194

In Windows, do this in the command prompt from the repo directory:

cd .git
del index.lock

UPDATE: I have found that I don't need to do this procedure if I wait a moment after I close out the files I'm working on before I try to switch branches. I think sometimes this issue occurs due to git catching up with a slow file system. Other, more git-knowledgeable developers can chime in if they think this is correct.

ssaltman
  • 3,623
  • 1
  • 18
  • 21
  • 3
    Strangely enough, I get the error about the file but then when I try to delete the file in cmd.exe, it says file not found :) Same result with absolute and relative paths. But in Git Bash, it works (with `rm -f index.lock`) – Juha Untinen Jun 27 '16 at 07:58
  • In my case, I executed "Fetch" after a couple of restarts of Sourcetree and worked. – maxivis Jun 18 '18 at 12:54
  • Though it won't be a problem in this case since we are dealing with a single file with 0 size, it is a better practice in general to use rmdir /s. del in cmd only removes files. Even then, without /s it won't recurse and will only remove files in the top-level directory. So, for cmd, you should be using rmdir /s to recursively delete all files and folders. – Onat Korucu Jun 10 '20 at 07:23
  • actually i have a react project, which needs to be uploaded everytime in the github, I always add it remotely, and i do it by "git init" but the problem is , there is already a .git file.., even though I delete that, this error still exists. – Rohan Devaki Oct 20 '20 at 18:03
37

Try quitting Xcode - since it's a git client, you have to quit Xcode to avoid problems with git on the command line.

Jeff Grimes
  • 2,300
  • 1
  • 23
  • 23
  • 4
    Nice! Note that Xcode AND other Git clients can be culprits. In my case it took closing gitX also. – NSTJ Jan 16 '15 at 00:04
  • After having to delete my lock file 10 times or so this morning I finally stumbled upon this answer. Don't know how xcode opened up but it was sure trashing my workflow. Thanks for the tip! – Cloudkiller Sep 15 '15 at 14:51
36

In my .git directory, there was no index.lock file. So, using the Git Bash shell, I ran...

cd .git
touch index.lock
rm index.lock

The touch command created the file, and the problem went away.

John Livermore
  • 30,235
  • 44
  • 126
  • 216
  • Why is this downvoted? I received the similar error message as OP. This fixed me, and maybe could help others. – John Livermore Sep 08 '16 at 12:33
  • 1
    I experienced a slight variant of this: (1) I experienced the error message and experimented with different 'fixes'; (2) I found that `index.lock` did not exist; (3) I used the above `touch` command; (4) my git client stopped functioning normally (at best, it slowed to a crawl); (5) I deleted `index.lock`; (6) the repository began functioning normally again. – ClarPaul Dec 26 '16 at 14:46
  • Definitely shouldnt be downvoted, sorted my problem out. I didnt have an index.lock file, but once I created one, and subsequently removed it, the commit worked. – Bynho Mar 17 '17 at 13:01
  • `touch index.lock` implies John was on *NIX, but I just did the equivalent on Windows 10 (create an `index.lock` file with a text editor, delete it immediately, and profit), and the problem's gone. – ruffin Aug 14 '17 at 21:54
  • It's downvoted because it's missing the last step of deleting the `index.lock` file that you just created with `touch`. – Spark Apr 16 '20 at 16:02
  • I keep getting `No changes - did you forget to use 'git add'?` – Pedro Paulo Amorim Jun 26 '20 at 14:46
  • Though it's a bit weird, why you have to create it and then delete it? But this is the only solution that worked for me. – JuLy Mar 09 '21 at 07:21
  • My Windows 10 with Git Bash working well – Bang Andre Oct 26 '21 at 14:53
  • yep, this is what worked for me also. issue on mac os and vs code – Claudiu Ungureanu Jan 04 '23 at 13:17
  • This is kind of absurd but it worked. – BadHorsie Jan 27 '23 at 10:44
34

I was having the same problem. I tried

rm -f ./.git/index.lock 

and the console gave me an error message. Then, I tried

rm --force ./.git/index.lock

and that worked.

Good Luck! This works super

Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65
Olivia Steger
  • 414
  • 6
  • 11
21

You need to delete the index.lock file.

In Linux

cd .git
rm -f index.lock

In Windows(10), do this in the command prompt from the repo directory:

cd .git
del index.lock
Maulik Kakadiya
  • 1,467
  • 1
  • 21
  • 31
15

Did you accidentally create the repository using the root user?

It just happens that I created the git repository as the root user.

I deleted the git repository and created it again without sudo and it works.

hktang
  • 1,745
  • 21
  • 35
10

i have this problem too, and i find it really a permission problem. so i do this:

sudo chown -R : .git #change group
sudo chmod -R 775 .git #change permission

then ererything is great, and gaa is success.

and then i use gp, i get another error almost the same error

sudo chown -R "${USER:-$(id -un)}" . #use this can fix the problem
defend orca
  • 617
  • 7
  • 17
9

In Windows, I only managed to be able to delete the lock file after Ending Task for all Git Windows (32bit) processes in the Task Manager.

Solution (Win 10)

1. End Task for all Git Windows (32bit) processes in the Task Manager

2. Delete the .git/index.lock file

9
  1. Run Task Manager, Locate Git for windows process and kill it.
  2. Delete index.lock file under .git folder
  3. It worked for me !
Sameh
  • 1,318
  • 11
  • 11
7

In Mac OS X do this in the command prompt from the repo directory:

cd .git
rm index.lock
techraf
  • 64,883
  • 27
  • 193
  • 198
Rick Wong
  • 73
  • 1
  • 5
7

The solution that worked for me was closing sublime text because the running git process was initiated by the editor.

snorkelzebra
  • 663
  • 1
  • 7
  • 17
  • I've recently installed the Sublime Text plugin GitSavvy and had this error only since then. Thanks for the hint, @snorkelzebra – Kitze Nov 10 '16 at 13:03
6

If after you try:

rm -f ./.git/index.lock

you get:

rm: cannot unlink 'index.lock': Permission denied

Try closing all software that might be using Git. I had Source Tree and Visual Studio opened and after closing both the command worked.

Boos
  • 127
  • 2
  • 9
6

I think there's a better solution than removing the file (and god knows what will happen next when removing/creating a file with sudo):

git gc
Roy Segall
  • 336
  • 2
  • 6
  • Because it's windows? Try to use cmder or maybe use git kraken which maybe fix some stuff – Roy Segall Aug 31 '20 at 19:24
  • in the middle of all these response all saying the same thing again and again (and `./.git/index.lock` no existing), this solved my issue :) – Kevin Feb 11 '21 at 18:16
5

If you are using one of #intelliJ IDEs and receiving this msg (i'm using #webtorm), please notice that this problem can occur because of hiding one of project folders (inside settings), and this can prevent GITfrom merging.

neoswf
  • 4,730
  • 6
  • 39
  • 59
5

In my case the solution was to wait 5 minutes. Obviously my previous operation was still running but I just didn't know it. I was using tortoise git on windows.

rdans
  • 2,179
  • 22
  • 32
5

If it's a submodule, try this instead on your repository directory :

rm -f ../.git/modules/submodule-name/index.lock

change submodules-name to your submodule name.

Kharda
  • 1,318
  • 14
  • 23
4

Use This:

rm -Force ./.git/index.lock
Manoj Tarkar
  • 438
  • 4
  • 9
3

DO NOT USE the Atom platformio-atom-ide-terminal Plugin to do this. USE THE TERMINAL OF YOUR DISTRO DIRECTLY.

I kept getting this error while rebasing/squishing commits and didn't know why because I had done it before several times.

Didn't matter how many times I would delete the index.lock file, every time it would fail.

Turns out it was because I was using the ATOM EDITOR terminal plugin. Once I used the terminal that ships with Ubuntu it worked like a charm.

juliangonzalez
  • 4,231
  • 2
  • 37
  • 47
3

You have problem with .git/index.lock so delete it using below command.

Command:

sudo rm -rf .git/index.lock

Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35
Pramod Kharade
  • 2,005
  • 1
  • 22
  • 41
3

I tried it with many methods several times but this one worked for me (I used PyCharm's terminal) :

$ cd .git/

$ rm -f index.lock

Then I tried again to create an empty git repo :

$ git init

$ git add .

$ git commit -m "commit msg"
2

The resolution for this problem is copying the three xcode/project files in the directory and then creating new directory (Whereever else) and then paste the three files/directories.

G unitzo
  • 89
  • 14
2

Also we can just kill git process. I receive the same issue via GUI app for git, something goes wrong and git makes some work infinitely. Killing process will freeze application that works with git, just restart it and everything will be ok.

kaspartus
  • 1,365
  • 15
  • 33
2

In case, for whatever reason, you are doing a rebase from a folder that's being sync'd by a cloud service (dropbox, drive, onedrive, etc), you should pause or turn off syncing as it will interfere with permissions during the rebase.

Dmase05
  • 1,059
  • 15
  • 18
2

For me it was

rm -r .git-credentials.lock 
N Jay
  • 1,774
  • 1
  • 17
  • 36
2

I had changed my directory permission so I knew it could be permission related. In my case I removed unwanted (_www) users and then applied read/write permission to everyone by apply changed to all contents. This is on Mac

Directory Permission on Mac

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
2

All the remove commands didn't work for me what I did was to navigate there using the path provided in git and then deleting it manually.

Aaron Rabinowitz
  • 357
  • 2
  • 18
2

In case someone is using git svn, I had the same problem but could not remove the file since it was not there!. After checking permissions, touching the file and deleting it, and I don't remember what else, this did the trick:

  • checkout the master branch.
  • git svn rebase (on master)
  • checkout the branch you were working on
  • git svn rebase
cauchi
  • 1,463
  • 2
  • 17
  • 45
2

A little adding because I had to use different answers to get the actual solution (for me).

This did it for me:

  1. Open branch you are working on
  2. Open terminal (I use terminal in Git GUI)
  3. Typ in command: cd .git
  4. Typ in command: rm -f index.lock

Some may have to use -Force instead of -f. You can check the command lines of your terminal by executing a command in your terminal something like: git help.

Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35
2

simply go to D:/project/androidgc/.git/ this directory and delete index.lock this worked for me.

avez raj
  • 2,055
  • 2
  • 22
  • 37
0

All the solutions are right:

Just remove .git from your corrupted repository, 

then copy this file if back from another clone (if you don't have it in another machine, just clone it).

Finally, what made the difference to me:

  • Avoid using sudo to unzip or copy the new .git folder. Git won't have access to the .git folder if you use superuser rights to create it
Javi
  • 913
  • 2
  • 13
  • 15
0

I had this occur when I was in a subdirectory of the directory corresponding to the root folder of the repo (ie the directory in which .git was). Moving up to the root directory solved the problem - at the cost of making all file references a bit more inconvenient as you have to go path/to/folder/foo.ext instead of just foo.ext

Motorhead
  • 928
  • 6
  • 16
0

A restart of the machine worked for me. None of the solutions provided so far worked for me.

Env Windows 10 + Git Bash

EDIT: I see the same solution was here: git index.lock File exists when I try to commit, but cannot delete the file

Michael
  • 308
  • 3
  • 12