claude-code - 💡(How to fix) Fix [BUG] Claude Code should use --no-optional-locks for all internal git operations and document statusline git pitfalls [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#47721Fetched 2026-04-15 06:44:10
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
labeled ×3commented ×1

Error Message

Fix: Claude Code's statusline documentation should explicitly warn against using git status and recommend either:

Root Cause

Root Causes

Fix Action

Workaround

For anyone hitting this now, replace git status --porcelain in your statusline with:

# Instead of: git status --porcelain
# Use:
if ! git --no-optional-locks diff --quiet HEAD 2>/dev/null || \
   ! git --no-optional-locks diff --cached --quiet 2>/dev/null; then
    git_dirty="*"
fi

Or prefix your entire statusline command with GIT_OPTIONAL_LOCKS=0:

{
  "statusLine": {
    "type": "command",
    "command": "GIT_OPTIONAL_LOCKS=0 ~/.claude/statusline.sh"
  }
}

Code Example

# Instead of: git status --porcelain
# Use:
if ! git --no-optional-locks diff --quiet HEAD 2>/dev/null || \
   ! git --no-optional-locks diff --cached --quiet 2>/dev/null; then
    git_dirty="*"
fi

---

{
  "statusLine": {
    "type": "command",
    "command": "GIT_OPTIONAL_LOCKS=0 ~/.claude/statusline.sh"
  }
}
RAW_BUFFERClick to expand / collapse

Problem

Stale .git/index.lock files continue to block git operations for Claude Code users. The original issue #11005 was auto-closed despite active discussion and no official fix.

This affects daily workflow — when the lock file appears, all git operations (commit, stash, rebase, checkout) fail until the user manually removes it.

Root Causes

1. Statusline scripts using git status --porcelain

The most common cause. Many statusline scripts (including official examples and community tools like claude-powerline) use git status --porcelain to show dirty state. This command acquires an index lock, and when it races with Claude Code's own git operations (or the user's), stale locks are created.

Fix: Claude Code's statusline documentation should explicitly warn against using git status and recommend either:

  • --no-optional-locks flag on all git commands
  • GIT_OPTIONAL_LOCKS=0 environment variable
  • Using git diff --quiet HEAD + git diff --cached --quiet instead of git status --porcelain

2. Claude Code's internal git polling

Claude Code runs frequent background git commands for context. These should all use --no-optional-locks to avoid competing with user operations and statusline scripts.

Verified Fix

Replacing git status --porcelain with git --no-optional-locks diff --quiet HEAD in my statusline script completely eliminates the issue. Stress tested with 30 concurrent invocations — zero lock files created.

Environment

  • macOS Darwin 25.4.0
  • Claude Code (latest)
  • Git 2.x
  • Large monorepo (~100k files)

Requested Changes

  1. Claude Code internal: Use --no-optional-locks for all background git operations (status polling, context gathering)
  2. Documentation: Add a warning to statusline documentation about git lock contention
  3. Default statusline: If Claude Code ships any default statusline with git commands, ensure they use --no-optional-locks
  4. Defensive cleanup: Consider auto-cleaning stale 0-byte lock files that are older than 30 seconds with no holding process (check via lsof)

Related Issues

Workaround

For anyone hitting this now, replace git status --porcelain in your statusline with:

# Instead of: git status --porcelain
# Use:
if ! git --no-optional-locks diff --quiet HEAD 2>/dev/null || \
   ! git --no-optional-locks diff --cached --quiet 2>/dev/null; then
    git_dirty="*"
fi

Or prefix your entire statusline command with GIT_OPTIONAL_LOCKS=0:

{
  "statusLine": {
    "type": "command",
    "command": "GIT_OPTIONAL_LOCKS=0 ~/.claude/statusline.sh"
  }
}

extent analysis

TL;DR

To fix the issue of stale .git/index.lock files blocking git operations, use the --no-optional-locks flag with git commands or set the GIT_OPTIONAL_LOCKS=0 environment variable.

Guidance

  • Replace git status --porcelain in statusline scripts with git --no-optional-locks diff --quiet HEAD and git --no-optional-locks diff --cached --quiet to avoid acquiring index locks.
  • Use the GIT_OPTIONAL_LOCKS=0 environment variable to disable optional locks for all git commands.
  • Update Claude Code's internal git polling to use --no-optional-locks to prevent competing with user operations and statusline scripts.
  • Consider adding a warning to Claude Code's statusline documentation about git lock contention.

Example

To replace git status --porcelain in a statusline script, use the following code:

if ! git --no-optional-locks diff --quiet HEAD 2>/dev/null || \
   ! git --no-optional-locks diff --cached --quiet 2>/dev/null; then
    git_dirty="*"
fi

Alternatively, prefix the statusline command with GIT_OPTIONAL_LOCKS=0:

{
  "statusLine": {
    "type": "command",
    "command": "GIT_OPTIONAL_LOCKS=0 ~/.claude/statusline.sh"
  }
}

Notes

This fix assumes that the issue is caused by the use of git status --porcelain in statusline scripts or Claude Code's internal git polling. If the issue persists, further investigation may be necessary.

Recommendation

Apply the workaround by replacing git status --porcelain with the suggested alternative or prefixing the statusline command with GIT_OPTIONAL_LOCKS=0, as this has been verified to eliminate the issue in the provided test case.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING