Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to linearize "splintered" merging history in Git?

Tags:

git

rebase

It must be very obvious, but I found no way to do it. Every manual describes rebasing onto a top of existing branch or simple interactive rebase and that's all. Suppose, I have a diamond-shaped git history like that:

*   949430f Merge commit (D) (HEAD, mybranch)
|\  
| * e6a2e8b (C)
* | 3e653ff (B)
|/  
*   9c3641f Base commit (A)

and i want to archieve history like:

*   949430f Combined commit (BCD)
|  
*   9c3641f Base commit (A)

Commits B and C may be melted or discarded at all, doesn't matter, I want to preserve only the result. Also I DO NOT want to revert commits because of nasty conficts resolving.

Here things I've tried:

1) I can't archeve that by simple squashing B and C

git rebase -i HEAD~2
...
p 3e653ff (B)
f e6a2e8b (C)
...
Could not apply a91f3a4

Well, that's somewhat understandable, there're some conflicts.

2) I can't archeve that by squashing.

git rebase -i -p HEAD~3
...
pick 9c3641f Base commit (A)
f 3e653ff (B)
f e6a2e8b (C)
pick 949430f Merge commit (D)
...
error: unable to match a91f3a4...

3) I can't even discard B and C

git rebase -i -p -m HEAD~3
...
pick 9c3641f Base commit (A)
#pick 3e653ff (B)
#pick e6a2e8b (C)
pick 949430f Merge commit (D)
...
error: Commit 949430f is merged but option -m is not set.
fatal: cherry-pick failed
Could not pick 4f3e6231b5cecf57434613ca3afd2a21ba375eb9

Why? Here is option "-m"...

Does anyone know how to solve this problem?

like image 263
ScalewingedAcidicorn Avatar asked Dec 05 '25 14:12

ScalewingedAcidicorn


2 Answers

What you want is git reset --soft (plus a commit as noted by aragaer). Despite the different circumstances in the question, see this answer by VonC.

like image 155
torek Avatar answered Dec 07 '25 07:12

torek


I'd try linearizing the commit graph first, and then squashing them together:

$ git checkout commitA -b newbranch
$ git cherry-pick commitB
$ git cherry-pick commitC
[resolve any conflicts]
$ git rebase -i commitA
[change the command for commitC to 'squash']
like image 33
musiphil Avatar answered Dec 07 '25 06:12

musiphil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!