Blog / git
Fix "index.lock: File exists" after a coding agent's git command
Git refuses to commit because .git/index.lock exists. Check nothing is still running, then remove the lock, including in a worktree.
On this page
- The error
- Check that nothing is still running
- Remove the lock
- In a worktree
- "cannot lock ref"
- Find every leftover lock
- A lock that keeps coming back
- Why git status still works
- Stop it happening again
- Where Joxo fits
- Frequently asked questions
- Is it safe to delete .git/index.lock?
- Why does git say "Another git process seems to be running"?
- Where is index.lock in a git worktree?
- How do I fix "cannot lock ref"?
- How do I stop two coding agents locking each other out?
- Why does the index.lock error keep coming back?
- Try it with your team
Your agent was in the middle of a commit when you stopped it, or two agents ran git in the same repository at once. Now every git add and git commit fails. We ran every command with git 2.43 on a scratch repository.
The error#
fatal: Unable to create '/home/dev/app/.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.Git creates index.lock while it changes the staging area and deletes it when done. If the process is killed halfway, the file stays, and git won't touch the index until it's gone.
Check that nothing is still running#
The lock may be real: an agent's commit can be waiting in an editor it opened in the background, or a long git rebase may still be going. List git processes:
pgrep -a gitIf one belongs to this repository, let it finish or stop it first. An agent stuck in an editor is covered in a coding agent stuck in git's editor. On macOS and Linux you can also ask which process has the file open:
lsof .git/index.lockNo output means nothing holds it.
Remove the lock#
rm .git/index.lockThen run the command again. Nothing in the repository is lost: the lock is an empty or half-written copy of the index, and git starts a fresh one. Check git status afterwards in case the interrupted command left some files staged and others not.
In a worktree#
Each git worktree has its own index, so its lock isn't in the project's .git folder. Ask git where it is from inside the worktree:
git rev-parse --git-path index.lock/home/dev/app/.git/worktrees/agent-fix/index.lockrm "$(git rev-parse --git-path index.lock)""cannot lock ref"#
A branch can be left locked the same way:
fatal: cannot lock ref 'HEAD': Unable to create '/home/dev/app/.git/refs/heads/main.lock': File exists.Same check, then remove the file the message names:
rm .git/refs/heads/main.lockFind every leftover lock#
A killed rebase or fetch can leave more than one lock behind. List them all, then delete them once you're sure no git command is running:
find .git -name '*.lock'.git/refs/heads/main.lock
.git/index.lockfind .git -name '*.lock' -deleteRun it from the top of the project. In a worktree, .git is a file that points to the real folder, so search the main repository's .git instead; it holds every worktree's locks under .git/.
A lock that keeps coming back#
If the error appears now and then and goes away on its own, something else is running git in the folder at the same moment. Common causes:
- your editor's git panel, which runs
git statusandgit fetchin the background; - a file watcher or build tool that calls git;
- a second agent, or a second session of the same agent, in the same folder.
Those locks last a fraction of a second, so deleting the file isn't the fix. Retry the command, and stop the second process from working in the same copy. Agents that give up on the first failure and then "fix" it by deleting the lock can break a commit another process is in the middle of, so tell yours to retry first.
Why git status still works#
git status only tries to refresh the index and skips the update if the lock is there, so it can look fine while add and commit fail. Don't take a clean status as a sign the lock is gone.
Stop it happening again#
- One agent per working copy. Two agents running git in the same folder will collide. Give each its own worktree or clone.
- Stop agents cleanly. Ask the agent to stop, or press Esc, rather than killing the terminal in the middle of a commit.
- No editors for agents. Set
GIT_in the agent's environment so a commit never waits on an editor that nobody can see.EDITOR=true
Where Joxo fits#
When several people's agents work on one project, each one runs on its owner's computer in its own copy of the repository, so they don't fight over one index. In Joxo, you see which agent is stuck and on what, from the desktop app or your phone. Setup is one line pasted into your agent.
Frequently asked questions#
Is it safe to delete .git/index.lock?#
Yes, once no git process is still running in that repository. The file only marks that the index is being changed; deleting it loses no commits or files.
Why does git say "Another git process seems to be running"?#
The file .git/index.lock exists. Either a git command is still running, for example a commit waiting in an editor, or one was killed and left the file behind.
Where is index.lock in a git worktree?#
Under the main repository's .git/worktrees/<name>/ folder. Run git rev-parse --git-path index.lock inside the worktree to get the path.
How do I fix "cannot lock ref"?#
Make sure no git command is running, then delete the .lock file named in the message, such as .git/refs/heads/main.lock.
How do I stop two coding agents locking each other out?#
Give each agent its own working copy, such as a git worktree, so they never share one index.
Why does the index.lock error keep coming back?#
Something else runs git in the same folder at the same moment: an editor's git panel, a file watcher, or a second agent. Retry the command, and give each agent its own working copy instead of deleting the lock.