Skip to content

Git Index Locked — Unable to Create Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Git Index Locked. We cover key concepts, practical examples, and best practices.

Git creates an index.lock file to prevent concurrent operations. If a previous command crashes or is interrupted, the lock file remains and blocks all subsequent git commands with "Unable to create .git/index.lock: File exists".

The Problem

git commit -m "message"

Error:

fatal: Unable to create '.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.

Wrong Approach

# WRONG — deleting without checking for other git processes
rm -f .git/index.lock
# Could corrupt the repository if another git process is running

Right Approach

# Check if any git process is actually running
ps aux | grep git

# If no git process is running, remove the lock
rm -f .git/index.lock

Expected output:

$ rm -f .git/index.lock
$ git status  # Now works
On branch main
nothing to commit, working tree clean

Step-by-Step Fix

Step 1: Check for running git processes

ps aux | grep -E "git\s+(commit|rebase|merge|cherry-pick)" | grep -v grep

Step 2: If git is running, wait or terminate

# Wait for the other process to finish
# Or kill it if stuck
kill -9 <PID>

Step 3: If no git process is running, remove the lock

rm -f .git/index.lock

Step 4: Check for other lock files

# Also check for other git lock files
find .git -name "*.lock" 2>/dev/null

Step 5: Remove all stale lock files

find .git -name "*.lock" -delete

Step 6: Verify the repository is clean

git fsck

Step 7: Re-run the failed command

git commit -m "message"

Prevention Tips

  • Never forcefully close terminal during git operations
  • Use git commit with -m flag to skip the editor
  • Set a git editor timeout in .gitconfig
  • Use git worktree instead of multiple git repos to avoid accidental operations
  • Allow git commands to complete naturally before closing the terminal

Common Mistakes with index locked

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Is it safe to delete .git/index.lock?

Only if no other git process is running. Check with ps aux | grep git first. If a git process is running while you delete the lock, both operations may corrupt the index. When no git process is running, the lock is stale and safe to delete.

What other lock files exist in .git?

Besides index.lock, git creates HEAD.lock (branch operations), refs/heads/*.lock (ref updates), packed-refs.lock, rebase-merge/ directory (interactive rebase), and CHERRY_PICK_HEAD (cherry-pick). All are safe to remove when no git process is running.

How does git prevent concurrent modifications?

Git uses file-based locking. Before any operation that modifies the repository, git creates a .lock file. Other operations check for the lock and fail if it exists. This prevents race conditions and repository corruption from concurrent writes.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro