2

I've discovered that I have a file committed to my repo year ago, which I never wanted to be there. Before I make my repo public I'd like to revert, or actually remove commit which introduced this file so no one will be able to access this file.

I've used interactive rebase so I was able to delete unwanted commit. It looked promising but after applying ~400 commits from 800 there is a conflict.

The conflict is caused by nonlinear history - there was a branch and one file was modified in both branches (lets say feature1 and feature2). As far as I've noticed git rebase tried to make this parallel history linear and it failed at those parallel, conflicting modifications. I don't want to try to fix it as there are much more such situations and It would be a big effort to fix all of them.

So my question is: is it possible to simply remove one commit with reconstruction of branch history without any influence on other commits?

My file was added once and was never modified, so none of other commits touch it.

Michał Walenciak
  • 4,257
  • 4
  • 33
  • 61
  • 2
    Related: [Remove sensitive files and their commits from Git history](http://stackoverflow.com/q/872565/456814), [How to remove/delete a large file from commit history in Git repository?](http://stackoverflow.com/q/2100907/456814), [Completely remove file from all Git repository commit history](http://stackoverflow.com/q/307828/456814), [Remove file from git repository (history)](http://stackoverflow.com/q/2164581/456814). –  Apr 20 '14 at 17:52
  • By default, `git rebase` does not preserve merge commits. However, [you can tell it to try to preserve merge commits](https://www.kernel.org/pub/software/scm/git/docs/git-rebase.html#_options) by using the `--preserve-merges` or `-p` flag. I'm not sure if this will re-apply previously made conflict resolutions though. –  Apr 20 '14 at 18:27

1 Answers1

2

You could try the filter-branch command.

git filter-branch --prune-empty --tree-filter 'rm <filename-to-delete> || true' HEAD

If the wrong commit just added the file to remove, the entire commit will be skipped (option --prune-empty).

I've tested this with a simple repo with a branch and a merge, and the merge was quite cleverly preserved. So you should not expect conflicts. And filter-branch creates a backup branch as well, sir. :-)

SzG
  • 12,333
  • 4
  • 28
  • 41