gitgitignoregit-rm

How do I make Git forget about a file that was tracked, but is now in .gitignore?


I put a file that was previously being tracked by Git onto the .gitignore list. However, the file still shows up in git status after it is edited. How do I force Git to completely forget the file?


Solution

  • .gitignore will prevent untracked files from being added (without an add -f) to the set of files tracked by Git. However, Git will continue to track any files that are already being tracked.

    Updated Answer in 2024

    Do NOT use git rm --cached <file> if you ever want to see that file again. It will remove it from git, and also your local machine.

    If you want to keep the file locally, but remove it from git tracking, use the answer by Konstantin. In short, use the following instead of git rm:

    git update-index --skip-worktree <file>

    However, according to the official git documentation:

    Users often try to use the assume-unchanged and skip-worktree bits to tell Git to ignore changes to files that are tracked. This does not work as expected, since Git may still check working tree files against the index when performing certain operations. In general, Git does not provide a way to ignore changes to tracked files, so alternate solutions are recommended.

    Therefore, you should still consider using the original answer below.

    Original Answer

    WARNING: This will remove the physical file from your local machine and other developers' machines on your or their next git pull.

    To stop tracking a file, we must remove it from the index:

    git rm --cached <file>
    

    To remove a folder and all files in the folder recursively:

    git rm -r --cached <folder>
    

    The removal of the file from the head revision will happen on the next commit.