Search code examples
gitgit-filter-branch

How to rewrite git history up to a merge commit included


I am trying to rewrite a part of a git repo history using filter-branch.

The history looks like this:

branch 1  A---B---E---F---G(HEAD)
                 /
branch 2  C---D-/

I am trying to rewrite all the commits starting from E included, up to the tip G.

So far, I came to the following command:

git filter-branch -f --env-filter "......" E..HEAD

Unfortunately (and according to the doc) this do not rewrite commit E.

Using the range B..HEAD do not work either as it rewrites all commits from the branch 2.

How am I supposed to rewrite all commits including E up to the tip? If that matters, branches 1 and 2 are completely unrelated.


Solution

  • There is a little known revision notation that can achieve this:

    git filter-branch -f --env-filter "......" E^! HEAD
    

    E^! includes E, but excludes all its parents. By specifying HEAD as well, you get everything reachable from HEAD (and E), but excluding everything reachable from the parents of E.