Skip to main content

間違って ignore すべきファイルをあげてしまった

問題

まちがって node_modules__pycache__ を commit & push してしまった。

しかし、その後、node_modules を含む .gitignore ファイルを commit & push してしまったので、 消すこともできない。

どうしますか?

解決方法

こんな感じでリモートだけ消せるはず。

git rm --cached node_modules

そして、 .gitignore に含まれているなら、以降 git add -A などで追加されることはない。

手順: __pycache__ 除外の例

bash
# 1. .gitignore に追加
echo '__pycache__/' >> .gitignore
git add .gitignore

# 2. リモート上でまだ追跡されている __pycache__ を確認
git fetch origin
git ls-tree -r origin/master --name-only | grep '__pycache__'

# 3. 該当ディレクトリをキャッシュから削除(追跡解除)
git rm --cached -r tools/__pycache__

# 4. コミット&プッシュ
git add -A
git commit -m "Remove __pycache__ from git tracking"
git push

# 5. リモートの状態を再確認
git fetch origin
git ls-tree -r origin/master --name-only | grep '__pycache__'