I had a file that I accidentally inserted a few commits ago (java_pid1112.hprof) and had to fully remove it from history. Here’s how I removed it (Note: If you have pushed to a remote, then see the link at the end of this post):

  1. Find the hash of the commit that the file was added in (in my case it was it was 4b2df66...)
  2. Run, git filter-branch --force --index-filter 'git rm --ignore-unmatch java_pid1112.hprof' --prune-empty 4b2df66..HEAD

Basically, the above command runs git rm --ignore-unmatch java_pid1112.hprof in every commit from “4b2df66” to “HEAD”. If the removal of that file causes a commit to be empty, then --prune-empty will delete that commit from history too.

I didn’t pull this command magically from nowhere, so here’s the two great sources I found in solving this issue. I had to use a little bit of info from both to achieve what I wanted.

Now a quick word from a tech company who helps support me and this site: