Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accidentally branched off of the wrong branch, and when I want to merge into the master I have to merge both branches

People also ask

Can you merge a branch of a branch to master?

Once the feature is complete, the branch can be merged back into the main code branch. First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch.

What happens when you merge a branch into another branch?

As you create commits in the new branch, Git creates new pointers to track the changes. The latest commits are now ahead of the main branch commits. As you continue to make commits, each branch keeps track of its version of files. Git knows which branch you have checked out by using a special pointer called HEAD.


Try git rebase --onto with the following syntax:

To put branch2's changes on to the master without including branch1's

git rebase --onto master branch1 branch2

Relevant output from git help rebase:

Here is how you would transplant a topic branch based on one branch to another, to pretend that you forked the topic branch from the latter branch, using rebase --onto.

First let’s assume your topic is based on branch next. For example, a feature developed in topic depends on some functionality which is found in next.

              o---o---o---o---o  master
                   \
                    o---o---o---o---o  next
                                     \
                                      o---o---o  topic

We want to make topic forked from branch master; for example, because the functionality on which topic depends was merged into the more stable master branch. We want our tree to look like this:

              o---o---o---o---o  master
                  |            \
                  |             o'--o'--o'  topic
                   \
                    o---o---o---o---o  next

We can get this using the following command:

git rebase --onto master next topic

Keep in mind the risks and pitfalls of rebasing, mentioned here: http://git-scm.com/book/en/Git-Branching-Rebasing


One way to do it is, use cherry-pick. Do a git log branch2 and find the commit id's you want and then switch to master branch using git checkout master then use git cherry-pick <commit_id>

Refer to this post for more details

How to merge a specific commit in Git