Problem

You screwed up and you added a bunch of garbage files to your repository and realized it while reviewing your PR (hopefully). This doesn’t happen often so you don’t remember how to make git stop paying attention to those files. You update your .gitignore file and it doesn’t help.

Gotcha

Sigh…

I always forget how to do this, so I am documenting it finally.

First and foremost I want to say that this has been answered in many places already. I just want to expand on the solution rather than just dropping two commands here and calling it a day.

StackOverflow

As usual, StackOverflow has the answer here. This answer is enough to be honest, but here are some guidelines to understand how you got to this point.

How did this happen?

Like it is stated in the intro a mistake was made or maybe you changed your mind about versioning those output files or what have you. Regardless of the reason here is what happened. When you use the git add -A command it does exactly what it says, it takes untracked files and adds them to your repository. After you perform your commit, it’s now a part of your repository. Even if you update your .gitignore at this point, it’s not going to stop tracking those files because they are part of the repository. That’s the key here “Part of the repository”.

How do I remove these items from the repository?

The only thing you can do to remove items from the repository is to run the “git rm” command.

git rm

There are two ways to handle this command:

  • Specific files by name or wild card
  • Whole folders

Specific files

You can pin point a file or you can provide a wild card to remove a bunch of files that match a pattern.

git rm --cached /Folder1/Folder2/output.txt
git rm --cached /Folder1/Folder2/output*

Whole folders

This will remove all files inside of a folder recursively

git rm -r --cached /Folder1/Folder2/OutputFiles/

Finally

Finally make sure that you update your .gitignore so that this doesn’t happen again.

Leave a Reply

Your email address will not be published. Required fields are marked *