Removing Files from a Git Repository Without Actually Deleting Them

October 18, 2009
git_merge_11

Sometimes when you first install git you realize you added a bunch of crap to your repository, which makes it bloated. Or maybe you have junk files like .DS_STORE lying around that you want to keep out of your git goodness.

Here’s how to delete from your repository without deleting them from your disk.

# git rm -rf --cached $FILES

Then commit, push, and then pull on any remote repositories.

If you want to delete them from your disk as well you can just omit the --cached bit.

# git rm -rf $FILES

Finally, to keep this from happening in the first place, you can use a .gitignore file to keep certain content from going into your repository. Just wildcard the stuff you’re looking for with asterisks and make a list of those strings in your ignore file.

supporting = loving

Since 1999 I've been creating ad-free technical tutorials and essays here. It's a one-person effort that's also my life and livelihood. If it makes your day more livable in any way, please consider supporting the work with a monthly or one-time donation. Your support means a lot to me, and makes all the difference. 🫶🏼