Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial - Merge two branches into a 3rd new branch

Is it possible to, instead of merging one branch into another existing branch, merge 2 branches into a 3rd new branch?

like image 911
Acorn Avatar asked Mar 22 '11 04:03

Acorn


1 Answers

Just merge your 2 existing branches and consider the merge as the tip of the 3rd new branch and the previous heads of the merged branches as your 1st and 2nd branch:

o    changeset:   3:92692c4a6b12
|\   bookmark:    masala
| |  summary:     merge salt and pepper
| |
| o  changeset:   2:a5f955adf03d
| |  bookmark:    pepper
| |  summary:     add some pepper
| |
o |  changeset:   1:2b56f2dc115f
|/   bookmark:    salt
|    summary:     add some salt
|
o  changeset:   0:e992ce7dd508
   summary:     initial

Here bookmarks have been used to mark different lines in development. So if you want to work in the new 3rd branch, update to masala, if you want to work on your 1st branch, update to salt, and similar for the 2nd branch update to pepper before you continue to work and commit.

If you prefer working with named branches (instead of bookmarks), just issue a hg branch masala before you commit the merge of revision 2 and 1.

The basic message is that although the graph only has one head, you are free to interpret it as 3 different lines of development.

Now, let's say you want to continue the work in the 2nd branch, pepper:

$ hg up pepper
... hack ...
$ hg ci -m "need more pepper"

And then you have some ideas for the salt thing:

$ hg up salt
... hack ...
$ hg ci -m "less salt please"

Now the history graph shows your 3 branches more clearly:

o  changeset:   5:d1f8eb72119a
|  bookmark:    salt
|  summary:     less salt please
|
| o  changeset:   4:acc9b01f584f
| |  bookmark:    pepper
| |  summary:     need more pepper
| |
+---o  changeset:   3:92692c4a6b12
| |/   bookmark:    masala
| |    summary:     merge salt and pepper
| |
| o  changeset:   2:a5f955adf03d
| |  summary:     add some pepper
| |
o |  changeset:   1:2b56f2dc115f
|/   summary:     add some salt
|
o  changeset:   0:e992ce7dd508
   summary:     initial

An alternative to bookmarks and named branches is to use different clones for individual branches. That is you clone your repo with the unmerged branches and merge them in the clone. Which approach is best, depends on your specific workflow and personal preferences.

like image 138
Oben Sonne Avatar answered Oct 20 '22 00:10

Oben Sonne