Skip to contentSkip to conversation

rewrite history

DISCUSSION Ilya Basin 4 participants 9 messages Jun 15, 2016

lore
Share & tools
lore

Ilya Basin

author
#0 lore raw

Hi list. There were 2 branches. One's HEAD was modified to match a specific commit at another branch. Now, how to merge them according to this scheme?

A---B---X---E---F

                     =>  C---D---X---E---F

C---D---X'

X and X' have no difference. I tried to write a script to cherry-pick E and F, but some of commits are merges and cherry-pick fails.

#1 lore raw
Quoted 10 lines · Ilya Basin

Hi list. There were 2 branches. One's HEAD was modified to match a specific commit at another branch. Now, how to merge them according to this scheme?

A---B---X---E---F

                     =>  C---D---X---E---F

C---D---X'

X and X' have no difference. I tried to write a script to cherry-pick E and F, but some of commits are merges and cherry-pick fails.

I think you just want to rebase using the "-p" option to preserve merges. Something like:

  $ git checkout -b rebased-branch F
  $ git rebase -p --onto D B

that will pick X, E, and F, and replay them on top of D, resulting in the graph you showed above. You could also do:

  $ git rebase -p --onto X' X

if you wanted to keep X' instead of X.

The new history will be stored on "rebased-branch". You can make sure it looks good to you, and then:

  $ git branch -f my_original_branch rebased-branch

to move the state over to your original branch. You could also just do the rebase directly on the original A-B-X-E-F branch.

-Peff

#2 lore raw
Quoted 13 lines · Jeff King
Quoted 10 lines · Ilya Basin

Hi list. There were 2 branches. One's HEAD was modified to match a specific commit at another branch. Now, how to merge them according to this scheme?

A---B---X---E---F

                     =>  C---D---X---E---F

C---D---X'

X and X' have no difference. I tried to write a script to cherry-pick E and F, but some of commits are merges and cherry-pick fails.

Quoted 8 lines

I think you just want to rebase using the "-p" option to preserve merges. Something like:

  $ git checkout -b rebased-branch F
  $ git rebase -p --onto D B

that will pick X, E, and F, and replay them on top of D, resulting in the graph you showed above.

Eh, careful. Nobody said the change between B and X is any similar to the change between D and X'. Replaying the changes E and F introduce on top of X' to arrive at C--D--X'-E--F is the best you could do, i.e.

Quoted 3 lines
  $ git rebase -p --onto X' X

if you wanted to keep X' instead of X.

is more like "even if you wanted to keep X instead of X'".

If you prefer commit message of X over X', you can rebase -i it after you are done the first round to get rid of A and B, though.

But wouldn't filter-branch a better tool for this? Graft to pretend that the parent of X is D instead of B, and filter the branch with F at its tip, that is.

#3 lore raw
Quoted 17 lines · Junio C Hamano
Quoted 17 lines
Quoted 10 lines

Hi list. There were 2 branches. One's HEAD was modified to match a specific commit at another branch. Now, how to merge them according to this scheme?

A---B---X---E---F

                     =>  C---D---X---E---F

C---D---X'

X and X' have no difference. I tried to write a script to cherry-pick E and F, but some of commits are merges and cherry-pick fails.

I think you just want to rebase using the "-p" option to preserve merges. Something like:

  $ git checkout -b rebased-branch F
  $ git rebase -p --onto D B
Quoted 6 lines
Quoted 2 lines

that will pick X, E, and F, and replay them on top of D, resulting in the graph you showed above.

Eh, careful. Nobody said the change between B and X is any similar to the change between D and X'. Replaying the changes E and F introduce on top of X' to arrive at C--D--X'-E--F is the best you could do, i.e.

I thought that was exactly what Ilya said with "X and X' have no difference". I assumed that meant "they are semantically similar commits on different bases" (i.e., a cherry-pick) and not "they have the exact same tree state" (i.e., "git diff X X'" is empty).

Quoted 3 lines

But wouldn't filter-branch a better tool for this? Graft to pretend that the parent of X is D instead of B, and filter the branch with F at its tip, that is.

If my assumption on the meanings is reversed (i.e., X and X' really are the same tree state, not introducing equivalent commits), then yeah, that would be better.

-Peff

author
#4 lore raw
Quoted 17 lines · Junio C Hamano
Quoted 17 lines
Quoted 17 lines
Quoted 10 lines

Hi list. There were 2 branches. One's HEAD was modified to match a specific commit at another branch. Now, how to merge them according to this scheme?

A---B---X---E---F

                     =>  C---D---X---E---F

C---D---X'

X and X' have no difference. I tried to write a script to cherry-pick E and F, but some of commits are merges and cherry-pick fails.

I think you just want to rebase using the "-p" option to preserve merges. Something like:

  $ git checkout -b rebased-branch F
  $ git rebase -p --onto D B
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

that will pick X, E, and F, and replay them on top of D, resulting in the graph you showed above.

Eh, careful. Nobody said the change between B and X is any similar to the change between D and X'. Replaying the changes E and F introduce on top of X' to arrive at C--D--X'-E--F is the best you could do, i.e.

JK> I thought that was exactly what Ilya said with "X and X' have no JK> difference". I assumed that meant "they are semantically similar commits JK> on different bases" (i.e., a cherry-pick) and not "they have the exact JK> same tree state" (i.e., "git diff X X'" is empty).

Quoted 3 lines
Quoted 3 lines

But wouldn't filter-branch a better tool for this? Graft to pretend that the parent of X is D instead of B, and filter the branch with F at its tip, that is.

JK> If my assumption on the meanings is reversed (i.e., X and X' really are JK> the same tree state, not introducing equivalent commits), then yeah, JK> that would be better.

JK> -Peff

sorry,
git diff X X' is empty

#5 lore raw
Quoted 7 lines · Ilya Basin

JK> I thought that was exactly what Ilya said with "X and X' have no JK> difference". I assumed that meant "they are semantically similar commits JK> on different bases" (i.e., a cherry-pick) and not "they have the exact JK> same tree state" (i.e., "git diff X X'" is empty).
[...]
sorry,
git diff X X' is empty

Oh, then ignore everything I said. :)

I think you want to graft and filter-branch as Junio mentioned.

-Peff

author
#6 lore raw
Quoted 2 lines
Quoted 2 lines

sorry,
git diff X X' is empty

JK> Oh, then ignore everything I said. :)

JK> I think you want to graft and filter-branch as Junio mentioned.

JK> -Peff

I'm puzzled. How to undo an unfortunate attempt of filter-branch? git log origin/master returns irrelevant info, although I didn't push it to origin and I also tried git branch -D master, -rD master and git fetch -f

author
#7 lore raw
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

sorry,
git diff X X' is empty

JK>> Oh, then ignore everything I said. :)

JK>> I think you want to graft and filter-branch as Junio mentioned.

JK>> -Peff

IB> I'm puzzled. How to undo an unfortunate attempt of filter-branch? IB> git log origin/master returns irrelevant info, although I didn't push IB> it to origin and I also tried git branch -D master, -rD master and git IB> fetch -f
Nevermind, forgot to rm .git/info/grafts

#8 lore raw
In reply to Ilya Basin
Quoted 6 lines · Ilya Basin

Hi list. There were 2 branches. One's HEAD was modified to match a specific commit at another branch. Now, how to merge them according to this scheme?

A---B---X---E---F

                     =>  C---D---X---E---F
Quoted 8 lines

C---D---X'

X and X' have no difference. I tried to write a script to cherry-pick E and F, but some of commits are merges and cherry-pick fails.

...

git diff X X' is empty

It sounds like you really want to apply X--E--F on top of D, but I don't know how you can do that since you need to do *something* to D in order to get a tree that matches X, and that something is (presumably) *different* from the change that X applied to B. So if

	diff B X  !=  diff D X'

then I think Junio's right that the best you can do is change the meta-data in X' (commit message, Author, etc) to match X then apply E and F on top of that. But filter-branch seems like overkill to me here -- I'd just use "rebase -p" but not quite as Peff described:

git checkout X'
git commit --amend .... # Replaces X' with X"
git rebase -p --onto HEAD X F

Giving you

	C---D---X"---E---F

where

	diff D X'  ==   diff D X"

and meta_data(X") == meta_data(X)

But if you're happy with C---D---X'---E---F then you can skip the "git commit --amend" step.

		M.