How to ignore files which are in repository?

GitGitignoreGit Rm

Git Problem Overview


I have a file (config.php), that is already committed to a Git repository, but I want to ignore locally, i.e. I want that file to remain in repository, but force Git to ignore any changes to it.

I put the file into .gitignore, but it is still marked as changed and Git still is attempting to commit changes to it, every time I commit something.

Any ideas, what am I missing or doing wrong?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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 

Solution 3 - Git

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>.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprcaenView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - Gitshiva kumarView Answer on Stackoverflow
Solution 3 - GitPhilip OakleyView Answer on Stackoverflow