1

I have the following situation:

  • I have 2 branches: X and Y;
  • After some time a decided to merge them;
  • After merging Y in X, I decided to add one more commit in X;
  • Now, in X the last 2 commits are: 1) merge commit, 2) the second commit that I decided to add;

The question is, how can I merge these 2 last commits?

I used git rebase -i HEAD~1, but I can't see the merge commit. Which is the alternative? I do this for making history clear.

3
  • From what branch did you run the git rebase? Commented Mar 6, 2020 at 19:33
  • @Thomas Bartelmess, from X Commented Mar 6, 2020 at 19:41
  • git rebase ... rectifies history (makes it linear as much as possible). That's why the merge commit disappears. There is no way to redone the merge and keep the history at the same time. Commented Mar 6, 2020 at 20:15

1 Answer 1

1

If what you would like to do is take back the merge and apply X before the merge of Y:

git checkout X~2 # go to revision before merge git cherry-pick X # apply the revision at the tip of X git merge Y -m "Merging Y" # if you like the results git branch -f X git checkout X 

PS Attending @0andriy's worries, it's good to notice that this rewrites history. If you have already pushed what you have on X to a repo where other people might have already started playing with it, it's considered rude to rewrite history. If, on the other hand, it's something that you haven't pushed anywhere, then feel free to rewrite history.

Sign up to request clarification or add additional context in comments.

4 Comments

Back merges is exactly a poison to the Git history to begin with. And history will be broken with this.
Ah, what? If this is something that hasn't been pushed yet (we don't know), it's perfectly fine to play with stuff.
Yes, start with wrong play we will encourage to repeat it IRL. At least you have to elaborate all this in your answer. And JFYI: there is no way to keep history with updating merge.
It is better now?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.