清除已提交的檔案,但包含在 .gitignore 中

有時它會發生 git 跟蹤檔案,但在稍後的時間點被新增到 .gitignore,以便停止跟蹤它。在新增到 .gitignore 之前忘記清理這些檔案是一種非常常見的情況。在這種情況下,舊檔案仍將在儲存庫中閒置。

要解決此問題,可以執行幹執行刪除儲存庫中的所有內容,然後重新新增所有檔案。只要你沒有掛起的更改並且傳遞了 --cached 引數,就可以執行此命令:

# Remove everything from the index (the files will stay in the file system) 
$ git rm -r --cached .

# Re-add everything (they'll be added in the current state, changes included)
$ git add .

# Commit, if anything changed. You should see only deletions
$ git commit -m 'Remove all files that are in the .gitignore'

# Update the remote
$ git push origin master