Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge/rebase a 'disconnected' branch in Git

Suppose we have the following situation in Git:

      X---Y feature
     /
A---B---C---D edge

Now I rebase the edge branch changing the B commit a little bit (using edit) so it now looks like this:

      X---Y feature

A---E---C'---D' edge

C' and D' are the same commits as C and D, but applied on top of E (and notice that X within the feature branch became disconnected).

Now how can I:

  1. Rebase/merge the feature branch so that its commits appear as if they were applied on top of D'?
  2. Rebase/merge the feature branch so that its commits appear as if they were applied on top of E, but without a separate 'merging branch ...' commit (and with C' and D' being rewritten to become C'' and D'')?
like image 536
Paweł Gościcki Avatar asked Mar 18 '11 09:03

Paweł Gościcki


People also ask

Does branch disappear after merging?

In a good workflow, the feature branch is deleted once its merged back into master. New branches should be created for each new feature(s) that you work on.

Does merging change both branches?

Merging Branches. Once you've completed work on your branch, it is time to merge it into the main branch. Merging takes your branch changes and implements them into the main branch. Depending on the commit history, Git performs merges two ways: fast-forward and three-way merge.

Which is better rebase or merge?

Merging is a safe option that preserves the entire history of your repository, while rebasing creates a linear history by moving your feature branch onto the tip of main .


1 Answers

X doesn't become disconnected per-se, it still has the original B as its parent. If you want to subsequently rebase feature on top of edge, then:

git checkout feature
git rebase edge

If you wish to change the tree so that it has a similar structure to the original version, but with X as a child of E, that's:

git checkout feature
git rebase --onto <sha-of-E> <sha-of-B> feature
like image 185
nickgrim Avatar answered Sep 25 '22 04:09

nickgrim