Make Git forget about committed files in .gitignore

Published on

Last Updated on

Estimated Reading Time: 1 min

git

Problem

You committed a file to git and then changed your mind and want git to forget about it. So, you added the file to .gitignore. But the file still keeps showing up as part of your changes.

Solution

This happens because .gitignore prevents files from being added but does not actively remove the ones already tracked.

To stop tracking a file (but not delete it), you need to remove it from the index. This can be achieved with this command.

git rm -r --cached <filename>
  • git rm: the command that lets us remove tracked changes.
  • -r: Removes the file recursively.
  • --cached: Removes only from the index.
  • <filename>: the file name you wish to stop tracking.

You can now make a new commit and push it to your remote.

Note that this will not remove the physical file from your local machine but will remove the files from other devices on the next git pull, so be careful with this command.

References