Running into this?

A commit fails with «Unable to create '.git/index.lock': File exists», and the same commit goes through a little later

I stopped my agents from taking the lock when they only read git: every agent starts with GIT_OPTIONAL_LOCKS=0. My commit no longer waits for an agent's git status.

beforeone agent's git status held the lock for 3–6 minutes
afterreading git takes no lock
put GIT_OPTIONAL_LOCKS=0 in your agents' environment
Text for your agent · 15 lines
Situation: while agents work in a git repository, your commit, another agent's commit, a `git add`, or your editor's source control fails with: `fatal: Unable to create '<repo>/.git/index.lock': File exists.` A few seconds or minutes later the same command goes through.

Why: a plain `git status` refreshes the index when file timestamps changed; to do that it takes `.git/index.lock` and rewrites the index. While the lock is held, a commit or add in the same working folder cannot take it (separate git worktrees have separate indexes). With a large untracked folder in the tree, status can run for minutes. For me, one agent's `git status` held the lock for 3–6 minutes and the commit in my editor failed. `GIT_OPTIONAL_LOCKS=0` turns off this optional lock: status only reads. Commit and add still take theirs. Observed on git 2.54.

Do:
1. In Claude Code's user settings (`~/.claude/settings.json`), add this for the agents' environment. If the file has no `env`, add `"env": { "GIT_OPTIONAL_LOCKS": "0" }`; if it has one, add only the `"GIT_OPTIONAL_LOCKS": "0"` member inside it.
2. Close and reopen running agent sessions: the environment is read when a session starts.

Verify (run in a reopened agent):
- echo "$GIT_OPTIONAL_LOCKS"
  expected: 0
- d=$(mktemp -d) && (cd "$d" && git init -q && echo a > f && git add f && git -c user.name=t -c user.email=t@t commit -qm i && touch -t 203001010000 f && touch -t 200001010000 .git/index && git status --porcelain >/dev/null && r=$(find .git/index -newermt 2001-01-01) && { [ -n "$r" ] && echo "index rewritten: yes" || echo "index rewritten: no"; }); rm -rf "${d:?}"
  expected: index rewritten: no

Don't: delete `.git/index.lock` by hand when you see the error; if a commit is holding it at that moment, the index can break.