0
2

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 pull ができない!?原因はキャッシュファイルだったので、ひとまず stash しました

Posted at

はじめに

Gitで git pull を実行した際に発生するエラーについて、対応方法をまとめました。

個人の備忘録程度の走り書きとなっておりますが、温かい目で見守っていただければ幸いです。

特に .pyc ファイル(Pythonのキャッシュファイル)に関連した問題の対処法です。

書こうと思ったきっかけ

開発中に git pull が失敗し、エラー内容を調べたところ .pyc ファイルのローカル変更が原因でした。このような問題はチーム開発でも起こりうるため、再発防止と共有のために記録しました。

Git Pull エラー対応メモ

エラー概要

error: Your local changes to the following files would be overwritten by merge:
        app/__pycache__/main.cpython-311.pyc
Please commit your changes or stash them before you merge.
Aborting

原因

  • Gitはマージ前に、ローカルの変更があるファイルがリモートで変更されていると上書きの可能性があるため、処理を止める。
  • 今回の対象ファイルは .pyc ファイル(Pythonのバイトコードキャッシュ)で、通常Gitで管理するべきではない。

解決策

✅ 方法①: .pyc ファイルをGit管理対象から除外(推奨)

git rm --cached app/__pycache__/main.cpython-311.pyc
echo "__pycache__/" >> .gitignore
git add .gitignore
git commit -m "Ignore __pycache__ directory"
git pull origin main

✅ 方法②: 一時的に変更を退避(stash)

git stash
git pull origin main
git stash pop

✅ 方法③: 変更を破棄(変更が不要な場合)

git restore app/__pycache__/main.cpython-311.pyc
git pull origin main

補足

  • .pyc ファイルや __pycache__/ ディレクトリは通常 .gitignore に追加し、Gitで追跡しないのが一般的。
  • すでにリポジトリに含まれている場合は、一度 git rm --cached で削除して、.gitignore に追加することで以後無視できる。

備考

  • チームメンバーにも .gitignore 更新をプッシュして共有すると良い。
  • もし他にも .pyc が混入している場合は、一括除外も検討。

まとめ

ここまで読んでいただきありがとうございました!最後に簡単に箇条書き程度でまとめメモを残しておきます。

  • .pyc ファイルはGitで管理しないのが基本。
  • pullできないときは、まず stash または restore を検討。
  • .gitignore を整備し、トラブルの再発を防ぐのが重要。
  • チーム開発では早めに共有・対応しておくと安心。
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?