user984621
user984621

Reputation: 48443

Git - fatal: Unable to create '/path/my_project/.git/index.lock': File exists

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?

Upvotes: 1076

Views: 840394

Answers (30)

Ashish Lahoti
Ashish Lahoti

Reputation: 978

I faced this issue during git commit on Windows. I manually deleted the file <project>/.git/index.lock and it worked.

Upvotes: 1

Glen
Glen

Reputation: 342

I Fixed this on Windows, my issue was the same but I needed to delete the HEAD.lock file as I didn't have an index.lock.

Upvotes: 0

Linh
Linh

Reputation: 60913

In my case, removing the lock file won't make it work.
Restarting the computer works for me

Upvotes: 0

Maulik Kakadiya
Maulik Kakadiya

Reputation: 1647

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

Upvotes: 30

Sameh
Sameh

Reputation: 1418

  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 !

Upvotes: 10

Michael
Michael

Reputation: 316

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

Upvotes: 0

John Livermore
John Livermore

Reputation: 31313

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.

Upvotes: 38

Motorhead
Motorhead

Reputation: 968

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

Upvotes: 0

Javi
Javi

Reputation: 1211

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

Upvotes: 0

hktang
hktang

Reputation: 1833

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.

Upvotes: 17

Olivia Steger
Olivia Steger

Reputation: 494

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

Upvotes: 42

ssaltman
ssaltman

Reputation: 3713

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.

Upvotes: 204

Kharda
Kharda

Reputation: 1368

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.

Upvotes: 5

avez raj
avez raj

Reputation: 2145

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

Upvotes: 2

user5683940
user5683940

Reputation:

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"

Upvotes: 3

Pramod Kharade
Pramod Kharade

Reputation: 2085

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

Command:

sudo rm -rf .git/index.lock

Upvotes: 3

Ronnie Oosting
Ronnie Oosting

Reputation: 1252

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.

Upvotes: 2

cauchi
cauchi

Reputation: 1543

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

Upvotes: 2

Roy Segall
Roy Segall

Reputation: 334

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

Upvotes: 5

juliangonzalez
juliangonzalez

Reputation: 4381

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.

Upvotes: 3

Aaron Rabinowitz
Aaron Rabinowitz

Reputation: 357

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.

Upvotes: 2

Chris-MindflowAU
Chris-MindflowAU

Reputation: 402

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

Upvotes: 10

defend orca
defend orca

Reputation: 743

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

Upvotes: 10

TheTechGuy
TheTechGuy

Reputation: 17354

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

Upvotes: 2

Boos
Boos

Reputation: 127

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.

Upvotes: 6

snorkelzebra
snorkelzebra

Reputation: 663

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

Upvotes: 7

Raphael R.
Raphael R.

Reputation: 24264

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.

Upvotes: 2226

Manoj Tarkar
Manoj Tarkar

Reputation: 452

Use This:

rm -Force ./.git/index.lock

Upvotes: 4

N Jay
N Jay

Reputation: 1824

For me it was

rm -r .git-credentials.lock 

Upvotes: 2

Rick Wong
Rick Wong

Reputation: 73

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

cd .git
rm index.lock

Upvotes: 7

Related Questions