Untrack files already pushed to git repository

This week, I pushed a file to a git repository which later on I realized would be better if the file is not tracked anymore. The file was the coverage/index.html generated by simplecov gem for my team’s Rails project. Everytime the unit tests are run in our local machine, this file gets updated. There’s no harm in pushing this kind of file but when doing pull requests, I just feel like this adds to the noise.

Adding a tracked file to .gitignore will not automatically remove it from being tracked. Here’s what I did to untrack it.

Step 1: Commit / stash all your changes

This step is not necessary but for precaution, make sure to commit / stash all your changes to avoid them from getting lost.

Step 2: Add the file to .gitignore

Append the relative path of the file you want to untrack to the .gitignore file.

At this point the file is still being tracked. You can check by doing git status. Notice that only the .gitignore file has changes.

Alt Text

Step 3: Remove the file from index

Remove the file from the index

$ git rm --cached <file_path>

When you check the git status, you’ll have something that looks like this Alt Text

The deleted file is the file that we want to untrack. It was removed from the cache but is still in our local machine.

Step 4: Add and commit the changes.

$ git add .
$ git commit -m '<commit_message>'

You have now removed your file from being tracked by git.