How to Fix “index.lock File exists” Error in Git
When git add or git commit stops with "Unable to create '…/.git/index.lock': File exists.", it means a file named index.lock is left over inside .git. After confirming that no other Git command is running, delete index.lock, and the same command will work again.
This error appears after a Git process has been forcibly terminated with kill -9 during something like git commit, and you then cannot commit anymore.
The test environment was Ubuntu 24.04, Git 2.43.0, and bash 5.2. I ran everything with LANG=C so the messages would be in English. The state where Git stopped midway was created by forcibly terminating git commit with kill -9. I did not test the behavior on Windows or macOS, or with Git built into editors such as VS Code.
The error message
In the test environment, I left index.lock in place and ran git add. It printed the following message and stopped with exit code 128. (The repository path is replaced with /path/to/repo.)
$ git add memo.txt
fatal: Unable to create '/path/to/repo/.git/index.lock': File exists.
Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
The second half of the message says: "Another git process seems to be running in this repository, for example an editor opened by 'git commit'. Make sure all processes have finished, then try again. If it still fails, a git process may have crashed in this repository earlier. To continue, remove the file manually." This text is in the Git 2.43.0 source code (lockfile.c), and it is printed when the file already exists.
In the same state, git commit -a -m 追記 and git restore --staged memo.txt also stopped with exactly the same message.
git stash ended with exit code 1 and printed no message. git stash list was empty, so no changes had been stashed. If git stash fails silently like this, check whether index.lock is left over.
On the other hand, git status, git diff --stat, and git log all displayed their results without errors.
What index.lock is
.git/index is the file that records the content to be included in the next commit (the staged content). See the git add reference and the gitrepository-layout entry for index. index.lock is a temporary working file that Git creates only while it is rewriting index.
According to the explanation of the lockfile mechanism in the Git source (lockfile.h), when Git changes a file, it first creates filename.lock, writes the new content there, and then renames it to the original name. If the .lock file already exists, creating it fails. This prevents two Git processes from rewriting index at the same time.

The same source also says that Git removes .lock files when the program exits or when it is stopped by a signal. In the test environment, when I stopped a git commit with an editor open using kill (SIGTERM), index.lock was not left behind. Only forcible termination with kill -9 (SIGKILL) left index.lock in place.
Before fixing it, check whether another Git process is running
This error appears not only when a stale file is left over but also when another Git process is actually running. In the test environment, I left a git commit -a editor open and ran git add while it was still open. It stopped with the same message as above.
In this case, running pgrep -a git to search for Git processes showed the git commit that had the editor open. (The number at the start is the process ID, which changes every time you run it.)
$ pgrep -a git
634 git commit -a -q
In this case, do not delete index.lock. Instead, write the commit message in the open editor and close it. In the test environment, closing the editor made git commit finish, index.lock disappeared, and the commit succeeded.
$ ls .git | grep lock
$ git log --oneline
cd5d861 メモを追記
066c6ed 最初のコミット
If you delete index.lock while another Git process is running, two Git processes could end up rewriting index at the same time. First use pgrep -a git or similar to confirm that no Git process is running.
If no Git process is running, delete index.lock
I tested this after forcibly terminating git commit with kill -9. pgrep -a git printed nothing (exit code 1), so no Git process was running. But index.lock was still left in .git.
$ pgrep -a git
$ ls .git | grep lock
index.lock
After deleting index.lock in this state, git add and git commit succeeded. The changes in the working tree (the changes to memo.txt) remained as they were after the deletion.
$ rm .git/index.lock
$ git status --short
M memo.txt
$ git add memo.txt
$ git commit -q -m 追記
$ git log --oneline
1489372 追記
cd5d861 メモを追記
066c6ed 最初のコミット
$ ls .git | grep lock
The last command, ls .git | grep lock, printed nothing, so index.lock is gone.
In a git worktree working directory, the index.lock location is different
In a working directory created with git worktree add, index.lock is not created at .git/index.lock. Instead, it is created inside .git/worktrees/<name>/ in the original repository. As described in the git worktree reference, each working directory has its own index. The path shown in the error message was also in that location.
$ git add memo.txt
fatal: Unable to create '/path/to/repo/.git/worktrees/wt/index.lock': File exists.
If you are not sure where the file is, run git rev-parse --git-path index.lock inside the working directory where the error occurred. It prints the path of index.lock. As described in the --git-path entry for git rev-parse, this option returns the actual location of a file inside .git.
$ git rev-parse --git-path index.lock
/path/to/repo/.git/worktrees/wt/index.lock
$ rm "$(git rev-parse --git-path index.lock)"
$ git add memo.txt
$ git status --short
M memo.txt
When I ran the same command in the normal working directory (the original repository), it printed .git/index.lock.
If the same "File exists" error appears with a name like refs/heads/main.lock instead of index.lock, the cause is in the branch records. That case is covered in the last section of a separate article, "How to Fix the cannot lock ref Error Caused by Branch Name Conflicts in Git."





