0

A few PR's ago, there was a change accidentally lumped into a commit that shouldn't have been. I'd like to revert one file from one merge several merges back and have it gone from history as well without impacting more recent merges. The file itself hasn't been edited in any other commits.

Arc
  • 1,028
  • 6
  • 16
  • So the file was changed, the changed file was added, a new commit was created, _and_ that commit was merged into another branch? Was either branch ever pushed? – matt Feb 02 '20 at 23:57
  • File was changed, lumped in with other files for a commit, pushed to a working branch, then merged to master. – Arc Feb 02 '20 at 23:58

1 Answers1

0

You can do this with git filter-branch

git filter-branch --tree-filter 'rm -f <filename>' HEAD

See Pro Git: Rewriting History for more.

But the BFG Repo Cleaner is easier and faster.

bfg --delete-files <filename> <repo dir>

If you want to rewrite a single commit that's now several merges back, and preserve those merges, use an interactive rebase with merges.

git rebase -i -m -r <the commit>^

Anyone working on top of that branch should git pull --rebase=merges to update their work on top of the changed history.

See this answer for more detail.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • I apologize for unclear wording. I don't want to delete the file itself, just have it revert to an older version of itself without history of its change. – Arc Feb 02 '20 at 23:59
  • @arc I think what you want is to remove the last edit to a file from history, which could be done with an interactive rebase but might get messy if there's a lot of branches since then. Could you edit your question with a concrete example? – Schwern Feb 03 '20 at 00:06
  • @Arc See also https://stackoverflow.com/questions/51127471/interactive-rebase-with-preserve-merges-of-all-commits-in-the-current-branch – Schwern Feb 03 '20 at 00:08