Search code examples
gitgit-branchgit-rewrite-history

Git: How to remove commit's parent commit


I performed some cleanups in a repository with BFG and ended with some unwanted artifacts as a result. At some point in the repo history, I have the following setting:

... -> A -> B -> C -> ... -> X -> Y -> Z -> ... -> HEAD/master
             \                   /
               P -> Q ----------

(both X and Q are Y's parents)

I turns out that P and Q are now both empty and detached commits. Y is a merge commit which is also empty (no changes, just have an additional parent). Between C and X there are many commits (including branches and merges, none related to the P->Q "branch".

I wanted to delete the P->Q commits and links altogether since they contains no changes at all. Something like the following as result:

... -> A -> B -> C -> ... -> X -> Y -> Z -> ... -> HEAD/master

I've googled about editing parents with rebase, replace, filter-branch but I'm having a hard time to get this right (I'm a novice git user).


Solution

  • Run git rebase --onto X Z to re-apply Z onto X (skipping y).