develop with

How to remove files from Git history

Opps you put some sensistive data in your git repo.

Oh shoot, I just pushed up a pull request with some sensistive or secret values in it. I’ve been there, it happens when you get a bit tired and are trying to get a deadline out the door.

No problem, git filter-branch to the rescue. It allows you to rewrite your history and then you will have to force the push up to the origin to rewrite the history remotely as well. The way to do this is to filter the branch and then remove the file from all the specific hashes and the git log.

Example with an application.properties file:

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch src/resources/application.properties' \
--prune-empty --tag-name-filter cat -- --all

This removes the application.properties from the cache and the index git uses to keep the history.

Or maybe you put something in a Dockerfile:

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch Dockerfile' \
--prune-empty --tag-name-filter cat -- --all

For more details check out Github’s Delete from repo page

comments powered by Disqus

Want to see a topic covered? create a suggestion

Get more developer references and books in the developwith store.