How to Fix the 'index.lock' Git Error in VS Code

beginner💻 VS Code2026-07-05| VS Code, Git, Windows, macOS, Linux

Error Message

fatal: Unable to create '.git/index.lock': File exists.
#git#vscode#devops#troubleshooting

The ProblemIt happens to the best of us: you’ve just finished a three-hour coding session and go to commit your work, only to be blocked by a stubborn error. You click the checkmark in VS Code’s Source Control panel or run git commit, but instead of a success message, you see this:

fatal: Unable to create '.git/index.lock': File exists.

This error puts a total halt on your workflow. You can't stage files, switch branches, or pull updates until it is resolved.

What's actually going on?Git uses a tiny, 0-byte file called index.lock as a safety mechanism. Think of it as a "Do Not Disturb" sign. It ensures that only one process—like VS Code’s background sync or your manual terminal command—can change your repository at a time. This prevents your data from getting corrupted by overlapping updates.

Normally, Git creates this file, finishes its task in milliseconds, and deletes the file immediately. However, if a process crashes or gets interrupted, the lock file stays behind. This often happens if VS Code's auto-fetch triggers at the exact moment you try to commit, or if your laptop goes to sleep during a large git pull.

Solution 1: The Manual CleanupThe fastest fix is to simply delete the leftover lock file. Since the original process that created it is likely long gone, removing it tells Git that the repository is open for business again.

Using the VS Code TerminalOpen your integrated terminal (Ctrl + `) and run the command for your operating system:

macOS or Linux:

rm .git/index.lock

Windows (PowerShell):

del .git/index.lock

Note: If you get a 'Permission Denied' error, you may need to close VS Code entirely and run your terminal as an Administrator.

Solution 2: Killing Ghost ProcessesSometimes, trying to delete the file results in a "File in use" error. This means a Git process is still running in the background, refusing to let go. You’ll need to force it to stop.

Windows- Press Ctrl + Shift + Esc to open Task Manager.- Look for git.exe or Git for Windows. You might see several instances if Git is stuck.- Right-click each one and select End Task.### macOS/Linux- In your terminal, find the process ID (PID) by running: ps aux | grep git- Identify the PID (usually a 4 or 5-digit number like 4021).- Force the process to stop: kill -9 <PID>## Solution 3: Taming VS Code's Auto-FetchIf you see this error frequently, VS Code’s "Auto Fetch" is likely the culprit. By default, VS Code runs git fetch every 3 minutes to check for remote changes. If this background check overlaps with your manual commands, it triggers a lock conflict.

To disable this feature:

  • Open Settings (Ctrl + ,).- Search for git.autofetch.- Uncheck the box.You will now need to click the refresh icon in the Source Control view to see remote updates, but you'll significantly reduce file-locking headaches.

VerificationAfter removing the file, check if Git is back to normal by running git status. If it displays your branch info without errors, you're good to go. Try a test commit to be sure:

git add . 
git commit -m "testing lock fix"

Summary of Best Practices- Avoid force-closing your terminal or IDE during a large rebase or merge.- Keep your Git version updated (run git --version to check; anything older than 2.30 might have less efficient lock handling).- Exclude the .git folder from real-time antivirus scans, as these can occasionally lock the index while trying to scan for threats.

Related Error Notes