This is part of a Git Tips series. For the complete set for beginners and intermediate Git users, see our white paper
You have a file committed to Git called hack-to-fix-bug.ts that you previously needed in your repository, but don’t any more since your colleague Gandalf did a far better fix, in a fraction of the time.
Adding a file to the .gitignore file is a good way of ignoring untracked files, although you can override it by forcing the addition of the file to the index by using git add –f on the file.
But how can you get Git to ignore committed files, and prevent them from constantly being show as available to commit every time you edit them?
Warning: It’s important to note that the solution for this will ultimately delete the file from everyone else’s machine the next time they run a git pull. It can then be added back into their working files manually, and thereafter ignored by Git.
In this example, you have just one file to ignore.
Firstly, add the file to your .gitignore file via your file explorer:
... Add files to .gitignore...
This removes the file from the index (specified by the --cached parameter). It also marks it for deletion, but not from your disc:
git rm --cached hacks/hack-to-fix-bug.ts
Or use this version, which removes all files in the specified folder:
If you’re nervous, then see what will happen without making changes using this:
git rm --dry-run -r --cached hacks
Carry on changing other files, and commit. The ignored file will stay on your disc, and will be ignored from now on:
git commit –m "Happily ignoring the right files now"