如何忽略存储库中的文件?

我有一个文件(config.php) ,它已经提交给了 Git 存储库,但是我想忽略本地文件,也就是说,我希望该文件保留在存储库中,但是强制 Git 忽略对它的任何更改。

我将该文件放入 .gitignore,但它仍然被标记为已更改,并且每次我提交某些内容时,Git 仍然尝试提交对它的更改。

有什么想法吗,我错过了什么或做错了什么?

36853 次浏览

If the file is still displayed in the status, even though it is in the .gitignore, make sure it isn't already tracked.

git rm --cached config.php

If you just want to ignore it locally, you could also make it ignored by the git status:

git update-index --assume-unchanged config.php

As commented, do note that using --assume-unchanged might cause unwanted data loss as git stash resets the "ignored" files to the state in upstream, undoing local changes, without a warning or saving.

If the file is already in the repository, and hence the Index/Staging area, then an update to .gitignore won't change that situation. It would keep being committed.

To remove the file from the Index/Staging area use git rm <file>.

Ignore checked in file:

git update-index --assume-unchanged file

To revert

git update-index --no-assume-unchanged file

Revert All

git update-index --really-refresh