0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人的備忘録:Git commitでエラー!index.lockファイルを削除して解決してみた

Posted at

はじめに

Gitでコミットしようとした際に、以下のようなエラーに遭遇することがあります。

fatal: Unable to create '/Users/ユーザー名/Desktop/プロジェクト名/.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が一時的に使用するロックファイル index.lock が残ってしまっていることで発生するエラーです。本記事では、その原因と対処法について解説します。

書こうと思ったきっかけ

ローカルで作業中に、普段通り git commit を使おうとしたところ、このエラーに遭遇しました。

原因がわからず戸惑ったため、備忘録として整理しておこうと思い、この記事を書きました。

同じようにGitのロックファイルで困っている方の参考になれば幸いです。

解決方法

エラーメッセージにもあるように、まず確認すべきは「他のGitプロセスが動いていないか」です。以下の手順で対応できます。

ステップ1:Gitプロセスが動いていないか確認

ps aux | grep git

他にGit関連プロセスが動いていないことを確認します。

ステップ2:ロックファイルを削除

rm -f .git/index.lock

Gitリポジトリのルートディレクトリに移動し、上記コマンドでロックファイルを削除します。

ステップ3:再度コミットを実行

git commit -m "変更"

これでエラーが解消され、正常にコミットできるはずです。

まとめ

  • index.lock はGitの排他制御に使われる一時ファイルです。
  • Gitプロセスが途中でクラッシュした場合などに残ることがあります。

このエラーは初見だと驚きますが、原因と対処法がわかれば難しいものではありません。トラブルに直面したときは、冷静に一つずつ確認してみてください...!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?