git ref_error ai_generated true

error: unable to create '.git/refs/heads/main.lock': File exists

ID: git/lock-ref-failed

Also available as: JSON · Markdown
92%Fix Rate
94%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2 active

Root Cause

A stale .lock file is preventing git from updating a ref. Usually caused by a previously interrupted git operation (crash, kill, Ctrl-C).

generic

Workarounds

  1. 90% success Run git gc --prune=now to clean up stale refs and lock files
    git gc --prune=now
    git remote prune origin

    Sources: https://git-scm.com/docs/git-gc

  2. 92% success Delete the stale lock file and retry the operation
    # Find the lock file mentioned in the error message and remove it:
    rm -f .git/refs/heads/main.lock
    # Or for other refs:
    rm -f .git/HEAD.lock
    rm -f .git/refs/remotes/origin/main.lock
    
    # Find all stale lock files:
    find .git -name '*.lock' -type f
    # Verify no git process is running before deleting:
    ps aux | grep git

    Sources: https://git-scm.com/docs/git-gc

Dead Ends

Common approaches that don't work:

  1. Re-cloning the entire repository 80% fail

    Loses local branches, stashes, and work-in-progress. The fix is much simpler.

Error Chain

Frequently confused with: