Git - How to untrack files

Learn how to untrack files in your GitHub Repository

Have you ever sent a file to your repository instead of adding it to your .gitignore file? Or perhaps you added it after you make your commit and now want to “remove” it for security reasons.

In this post, I will show you how to untrack files from your repository (so no one can see them) but they won’t be deleted or removed from your system.

To stop tracking a single file you can use:

git rm --cached filename

Let’s say that you want to stop tracking your .env file from your repository, so you must run this:

git rm --cached .env

The next step is to record your changes, so let’s do this:

git add .
git commit -m ".gitignore is now working"

Another option is if you want to untrack every single file that is now in your .gitignore, for this case you should run this:

git rm -r --cached .

Please note that this also removes any changed files from the index or staging area.

Make sure to commit all your important changes before running git add . , Otherwise, you will lose any changes to other files.

Finally, be careful when you push this to a repository and pull it from somewhere else into a state where those files are still tracked, the files will be DELETED.

I hope you can find this tutorial helpful and let me invite you to follow me on Twitter and my personal blog.