alexandernst
alexandernst

Reputation: 15109

Merging merge commits

Let's say I have a repository that looks the following way:

       /--X---Y----Z---\
A----B----C---D---E-------F-----G---

My question is if I can "merge" the merge commits so that the repository would like like this:

A----B----C---D---E-------F+(X,Y,Z)-----G----H-----I----

or maybe

A----B----C---D---E-------Q-----G----H-----I----

if that would make it easier.

EDIT: I'd like to make the history of a repository linear, but I'd like to make it (somehow) programatically as the repo is huge (we're talking about hunders of commits) and I just can't rebase -i it.

Upvotes: 1

Views: 41

Answers (2)

hlovdal
hlovdal

Reputation: 28180

This can be done with git rebase --onto and using explicit, target and source arguments:

git branch branch-to-insert Z
git branch branch-following-insert I      # or preferably use the actual branch
                                          # name instead of "I"
git rebase --onto E B branch-to-insert    # rebase commits from (not including)
                                          # B, e.g. X, Y and Z
git rebase --onto branch-to-insert F branch-following-insert      # rebase G..I

This should give you a branch with

A----B----C----D----E----X----Y----Z----G----H----I----

For the full picture of your tree after the commands:

       /--X---Y----Z---\
A----B----C---D---E-------F-----G----H----I----
                   \---X---Y---Z---G---H---I---
                               ^           ^
                               |           branch-following-insert
                               branch-to-insert

Upvotes: 1

Sid
Sid

Reputation: 1144

I think what you're looking for is the rebase command. This allows you to avoid using a merge and create a linear history. Basically, this command lets you take all the changes that were committed on one branch and replay them on another one. Read more about it here.

But the normal rebase workflow would only work for you if you have not done the merge yet. From your question, it seems like you have and you're looking for a way to rewrite your history. Then I would suggest looking into interactive rebasing, which does exactly that. You can change multiple commit messages, reorder commits, squash commits and split commits. Read more about it here.

Update after the edit: Maybe look into the filter-branch option. I've personally never used it but it seems to be exactly what you're looking for. From Git's documentation:

"There is another history-rewriting option that you can use if you need to rewrite a larger number of commits in some scriptable way"

Upvotes: 0

Related Questions