1

The question is an extension of this one: How to inject a commit between some two arbitrary commits in the past?

Suppose I have the following commit history on my local-only branch:

A -- B -- C -- D  
            \- E

How do I insert a new commit between A and B and keep both D and E above C?

Robin Green
  • 32,079
  • 16
  • 104
  • 187
jota
  • 23
  • 7
  • Have you tried the answers to that question? – Robin Green Dec 02 '18 at 21:29
  • yes, in the answer I need to choose what branch (let say E or D) I want to rebase on A. But I want to rebase D and E at the same time, to avoid breaking the two branches and keep the common C. I know I can choose E and rebase -i on A, and then rebase --onto to add the E branches on C, but when there is lot of above branches, it's a lot of work. – jota Dec 02 '18 at 21:33
  • Yes, it is a lot of work because git does not directly support this kind of workflow. – Lasse V. Karlsen Dec 02 '18 at 21:39
  • I thought git allow history rewritting to have a clean project. So this is only true when I am on a single branch? after diverging there is no way to easily alter the history? – jota Dec 02 '18 at 21:55
  • Your solution is to use `git replace` and then `git filter-branch` (but hash of all the commits after A will change) – Philippe Dec 02 '18 at 22:09
  • git replace may solve the problem, but the goal here is to have a clean history that may be transferable to other project. I'm a bit afraid of the mess of replace. I Will try anyway. – jota Dec 02 '18 at 22:53

2 Answers2

1

You can do an interactive rebase on your first branch to inject your commit and then do a rebase onto to apply this changes to your other branch.

Assuming you have:

A -- B -- C -- D  
           \-- E

After injecting your commit into your first branch (following the How to inject a commit between some two arbitrary commits in the past) you would have something like:

A -- A2 -- B' -- C' -- D'
 \-- B  -- C  -- E

Than you can do a rebase --onto from your another branch, to apply the changes it:

git rebase --onto C' E branch2

So you final result would be:

A -- A2 -- B' -- C' -- D'
                   \-- E'
abarisone
  • 3,707
  • 11
  • 35
  • 54
Aurora Wang
  • 1,822
  • 14
  • 22
  • To better understand `rebase --onto` you can read [here](https://stackoverflow.com/q/29914052/10657880). – Aurora Wang Dec 02 '18 at 22:17
  • 1
    Thank you for the answer, but as I said in this comment https://stackoverflow.com/questions/53584752/how-to-inject-a-commit-between-some-two-arbitrary-commits-in-the-past-when-there/53585114#comment94033440_53584752 I know this solution, but it can be a huge work if there is lot of branches above C. I'm looking for a solution that does the same, but whithout the "104 branches, 104 rebase to do". Maybe the only solution is scripting. – jota Dec 02 '18 at 22:21
  • Have you seen [How can I rebase multiple branches at once?](https://stackoverflow.com/q/23386318/10657880)? There are some utilities made for this purpose. – Aurora Wang Dec 02 '18 at 22:23
  • thanks, that's may be a solution, but I can't manage to make it work. Assertion failed, some name are undefined. May be it's an unfinished work. – jota Dec 02 '18 at 22:58
0

Stop the madness :) and just add the commit to D and then either merge or cherry-pick it to E

A -- B -- C -- D -- X 
           \-- E -- X   
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • Yes I now that your solution is the productive one, where I can go back to work after two cherry-pick. But what if the ABC commit are common to hundreds of branches?can't I extend the ABC root to ABB'C in one operation? – jota Dec 03 '18 at 12:59
  • Yes it's true, but what if there is 23 branches aboce C? – jota Dec 03 '18 at 17:15
  • ...than it's even more risky to do it before C. How would you test a change in 23 branches? – tymtam Dec 04 '18 at 06:20
  • Ok I agree that seem to be a bad idea. But in this precise case I try to insert a commit that does an add in the makefile, and I want this new little feature everywhere when I crusing my history. But in a more general situation the intergration test are here for that, right? – jota Dec 04 '18 at 16:23