タイトル: Another git process seems to be running in this repository
SEOタイトル: git「Another git process seems to be running」.git/index.lock の原因と消し方
| この記事の要点 |
|
エラーメッセージ
fatal: Unable to create '/path/to/repo/.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. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
原因
Git はインデックス (ステージング領域) や参照を更新する間、排他制御用のロックファイルを .git/ 配下に作ります。代表的なものが .git/index.lock です。
正常に処理が終われば自動で削除されますが、以下の状況で残ったままになり、次の git コマンドがこのエラーで止まります。
- 前回の git コマンドを Ctrl+C で強制終了した
git commit中にエディタが落ちた / PC が再起動した- git プロセスがクラッシュした
- VS Code の GitLens / GitHub Desktop / IDE 内 git が同時に動いた
- Dropbox / OneDrive / iCloud などの同期サービスが
.git/を共有している - Antivirus が
.git/indexをスキャン中に新規 git がロックを取りに来た - ネットワークドライブ / NFS でロックファイル更新が伝播遅延
対処1: 本当に他の git が動いていないか確認
消す前に、本当に他の git プロセスが居ないかを必ず確認します。動いている最中に .git/index.lock を消すと、リポジトリが壊れる可能性があります。
# Linux / macOS
ps aux | grep git
# alice 12345 ... git status
# alice 12346 ... git commit -m "..."
# プロセス無し or 不要なら kill
kill 12345# Windows PowerShell
Get-Process git -ErrorAction SilentlyContinue
Get-Process | Where-Object { $_.Name -like 'git*' }
# 不要なプロセスを終了
Stop-Process -Name git -Force
VS Code の Source Control パネル、GitHub Desktop、SourceTree などの GUI クライアントが開いている場合は、これらも一旦閉じます。
対処2: .git/index.lock を削除
# Linux / macOS / WSL / Git Bash
rm -f .git/index.lock
# もう一度 git コマンド
git status
git commit -m "..."# Windows PowerShell
Remove-Item .git\index.lock -Force
# CMD
del /F .git\index.lock
対処3: 他のロックファイルも疑う
Git は処理内容に応じて複数のロックファイルを作ります。.git/index.lock 以外でも同じエラーが出ることがあります。
# .git/ 配下のロックファイルを全部探す
find .git -name '*.lock'
# 例
# .git/index.lock ← commit/add 時
# .git/HEAD.lock ← branch 切替時
# .git/refs/heads/main.lock ← push/fetch 時
# .git/ORIG_HEAD.lock ← merge/rebase 時
# .git/packed-refs.lock ← gc/pack 時
# .git/config.lock ← git config 編集中
# .git/shallow.lock ← shallow clone 関連
# 確実に他の git が居ないことを確認して全削除
find .git -name '*.lock' -delete# Windows
Get-ChildItem .git -Filter '*.lock' -Recurse
Get-ChildItem .git -Filter '*.lock' -Recurse | Remove-Item -Force
対処4: それでも直らないとき
.git/のパーミッションを確認 (ls -la .git/) — 別ユーザ所有になっていればsudo chown -R- ファイルシステムが満杯ではないか (
df -h) — ロックファイル削除すらできない場合がある - ネットワークドライブ / SMB / NFS にあるリポジトリはローカルディスクへコピーして試す
git fsckでリポジトリ整合性を確認- 最終手段: 別の場所に
git cloneし直し、未コミットの変更をマージ
# 整合性チェック
git fsck --full
# 緊急脱出: 作業ツリーを退避してクリーン clone
cd ..
mv broken-repo broken-repo.bak
git clone broken-repo
cd broken-repo
# broken-repo.bak から未コミットの変更を手作業でコピー
頻発する場合の根本原因
| 環境 | 原因の傾向 | 対策 |
|---|---|---|
| VS Code + GitLens | 定期的にバックグラウンド git が走る | GitLens 設定の gitlens.advanced.repositorySearchDepth を 0 に |
| GitHub Desktop / SourceTree | fetch を頻繁にバックグラウンド実行 | fetch 間隔を伸ばす、CLI と同時操作しない |
| WSL から /mnt/c リポジトリ | NTFS と ext4 のロック挙動差 | リポジトリを WSL 側 (/home/...) に置く |
| Dropbox / OneDrive | .git/ を同期して別 PC とロック競合 | 同期対象から .git/ を除外、そもそも .git/ を同期しない |
| Antivirus (ESET, Defender) | commit 直後のスキャンで遅延 | リポジトリパスを除外設定 |
| Cron / CI で並列 git | 同じ作業ディレクトリで複数 job | flock / 排他制御 / 別ディレクトリで clone |
予防: シェルスクリプトでの自動回避
CI / 自動化で git を走らせる場合、ロック残留に備えて安全に消すラッパーを書いておくと安心です。
#!/usr/bin/env bash
# safe-git.sh - 残留ロックを自動削除してから git を実行
set -euo pipefail
REPO_DIR="${1:-.}"
cd "$REPO_DIR"
# 動いている git が居ないかチェック
if pgrep -x git >/dev/null; then
echo "[ERROR] git process is running. abort." >&2
exit 1
fi
# 古いロックを削除
find .git -name '*.lock' -mmin +5 -delete 2>/dev/null || true
# 本コマンド
shift
exec git "$@"
# 使い方
# ./safe-git.sh /path/to/repo status
# ./safe-git.sh /path/to/repo pull --rebase
FAQ
Q: .git/index.lock を消したらリポジトリが壊れた
A: 別の git が動いていた可能性があります。git status で異常を確認し、git fsck で完全性をチェック。問題があれば git reflog から復旧、最悪は別の場所に clone し直してください。
Q: VS Code を開くたびに出る
A: GitLens / Git Graph などの拡張機能が起動時に複数の git コマンドを並列実行することがあります。拡張機能を一時無効化して再現するか確認、またはリポジトリを /mnt/c から WSL の /home に移動すると劇的に減ります。
Q: なぜロックファイルが必要?
A: Git のインデックスファイル (.git/index) はバイナリの単一ファイルで、複数プロセスが同時に書くと壊れます。ロックファイルでアトミックな更新を保証するための仕組みです。