Oscar the owl and Vershd logo

Git Ignore Committed Files

16th March 2020

This is part of a Git Tips series. For the complete set for beginners and intermediate Git users, see our white paper.

The Problem

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?

The Solution

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:
git rm -r --cached hacks
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"

Pushing to the Horizon

Do you want to use Git far more easily, with no commands or parameters to remember, and with a clear overview of your repository, plus the ability to preview changes? Then make your life easier, get a free download of Vershd, the effortless Git GUI.
Designed from the ground up to prevent errors, accidental deletions, and having to write all of this morning's code. Again.

Features include:
  • Rewind is simpler and easier than using Revert or Reset. It winds the clock back on commits, letting you choose whether to keep pending files or not.
  • Advanced Context Menus. Right clicking on a commit lets you merge, cherry pick, or create a branch there and then. Files have many ways to compare them to either historic or working files on your desktop.
  • See everyone's work in the clearest way possible. Branches shows branches and their commits, where you can easily pull, push, cherry pick and more. Pending shows what you can commit or stash. Files shows commits' details. Stashes helps you apply, view and delete your stashes.
Oscar the owl and Vershd logo
 © 2020 Blightysoft
Made in England
St. George's flag of EnglandThe Union Jack, the flag of the United KingdomThe European Union flagThe United Nations flag
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram