Removing Files from a Git Repository Without Actually Deleting Them

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.

Related posts: