Drop old commit: `git rebase` causes merge conflic

2019-01-19 23:16发布

问题:

Unfortunately we accidently checked in a large binary file some time ago and until today nobody noticed. Now I want to drop that commit and have the remaining history as it is. I know about the caveats of changing pushed history but in this case I cannot avoid it.

I have been trying to achieve that for ~1h but fail to get it. The best command I found is

git rebase --interactive --preserve-merges $(EVIL_COMMIT)^

and in the editor commenting out the 1st commit which is the evil one.

Unfortunately git rebase stops at merges and prompts for manual resolution of merge conflicts. The evil commit only adds some example files our software shall compute for testing purposes. Thus their shouldn't be any conflict with the example files just missing.

  1. I do not understand where the merge conflicts originate from. Somebody can explain?
  2. How to resolve that?

I've spent quite a lot of time at Google and SO search. Some threads cover a similar topic but either syntax used is not available in today's Git version anymore or it didn't work for me (I only described one method above because it's the easiest approach).

回答1:

I'd go with filter-branch:

git filter-branch --prune-empty --index-filter '
  git rm --cached --ignore-unmatch path/to/file
' --all


回答2:

My friend started a convenient script for purging files from git history, check https://github.com/donquixote/gitpurge