Untrack files added to Git repository after .gitignore update

So you have decided to exclude some files from your Git repository, you have added the relevant lines to the .gitignore, however Git is still tracking those files.

To stop this from happening you will need to update the Git index to reflect the changes you have made to your .gitignore, or in other words you will need to "untrack" the files.

This is pretty simple to do and can be done in 4 easy steps, using Git Bash.

Step 1
Make sure you have committed all your changes including the updated .gitignore.

Step 2
To clear the git index / cache run the following command:

git rm -r --cached .

A break down of the command into it's respective parts:

  • rm - This is the removal command
  • -r - This will make it work recursively
  • -cached - This will only remove files from the index / cache
  • . - This will untrack all files, you can also untrack a single file by replacing the . with the file name

Step 3
You now need to re-add everything to the Git index / cache by running the following command:

git add .

Step 4
Commit the changes to the repo:

git commit -m ".gitignore updates"

Now your repository will no longer track the files you added to the .gitignore.

Make sure to push the changes to your remote for that to be updated too!

git push
Comments (0)