How to make Git “forget” about a file that was committed but is now in .gitignore?

Ever happened to you that you intentional or not, committed a file to git and then changed your mind and wanted git to forget about it?

How to make Git “forget” about a file that was committed but is now in .gitignore?

Image from http://perforce.com

Ever happened to you that you intentional or not, committed a file to git and then changed your mind and wanted git to forget about it?

Well, if we didn’t commit it yet, we normally add it to the .gitignore list and so git will ignore it even if you make changes to it, it won’t show up as part of your changes when you do git status.

But, if you forget to add it to .gitignore How do you make git to forget about it?

To stop tracking a file you need to remove it from the index. You can do this by running the following command:

git rm --cached <file-name>

If you want to remove a folder, you can use the -r argument that removes all filed in that folder recursively for you, like the following command:

git rm -r --cached <folder-name>

After you did this, on your next commit, the file will be removed from the head revision, so it won’t be in git anymore!

NOTE: When you do this, it will not remove the physical file from your local (the computer that ran the command), but it WILL remove the files from other developers machines on next git pull.

If you use GIT regularly, I recommend to get this book and have it on your desk:

Pro Git
Pro Git (Second Edition) is your fully-updated guide to Git and its usage in the modern world. Git has come a long way…www.amazon.com

If you liked this article, click the👏 below so other people will see it here on Medium.

Let’s be friends on Twitter. Happy Coding :)

Cheers!