Ignore files that have already been committed to GIT

Ishara Ilangasinghe
2 min readMar 8, 2019

--

Have you committed files to GIT you really shouldn’t commit, like totally not!

Well say you accidentally did because you don’t have a proper .gitignore file or maybe you are working on a repo that needs some cleaning!

After committing a certain file/folder no matter how many times you properly modify the .gitignore, using different patterns you cannot remove a committed entry. This is because GIT can only ignore files that are untracked files that haven’t been committed to the repository, yet.

Then how to remove an already tracked file!?

Let’s start cleaning up the directory.

The current working copy should be clean so, commit/stash any local changes you have made to the repo.

In just 3 commands, you can make git forget the files it tracked and make the ones you wanna ignore, really ignored!

Command 01:

git rm -r — cached .
  • rm is the remove command.
  • -r will allow recursive removal.
  • - -cached will only remove files from the index. Your files will still be there.
  • The . indicates that all files will be untracked. You can untrack a specific file with
git rm — cached foo.txt

Now you can modify your .gitignore

Make sure it’s up-to-date and contains all the correct patterns you want to ignore. Unless you want to make the same mistake again. :p

Gitignore.io is a site, a friend recommended me when I was struggling creating patterns for my .gitignore

Command 02:

git add .

This will stage new files and modifications, (without deletions)

git status

Check status and see how all the previously tracked files are treated like new files and removed according the modified .gitignore

Command 03:

git commit -m “Clean up ignored files”

This is how you untrack, tracked files and own a properly cleaned up repo, with all the .gitignore files properly ignored!!!

--

--

Ishara Ilangasinghe
Ishara Ilangasinghe

Written by Ishara Ilangasinghe

Business Analyst | Speaker at Write the Docs Australia 2022 | Senior Technical Writer at WSO2 | Toastmaster | MBA | BEng

No responses yet