Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Test combination of two feature branches

I have a Git branch for each feature. However, sometimes, when developing in branch A, I would like to test how A would behave if B was applied also.

At the moment, I use this:

git checkout A
git branch base
git merge B
git reset --mixed base
git branch -D base

Is there a shorter way to do this?

Additionally: what if A and B do not share the same ancestor?

o -- o -- o [A]
 \-- x -- o [v1_0]
           \ -- b1 -- b2 -- o [B]

How could I do something like this more easily?

git checkout A
git branch ABase
git cherry-pick v1_0..B   // only applying b1,b2 not x
git reset --mixed ABase
git branch -D ABase
like image 886
schoetbi Avatar asked Sep 04 '15 10:09

schoetbi


People also ask

Can we merge 2 feature branches?

Merge branchesMerging your branch into master is the most common way to do this. Git creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

Can git work with two branches at once?

You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.

How do I combine two code branches?

Merging Branches in a Local Repository To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch.

How do I find out if two branches are the same?

In order to compare two branches easily, you have to use the “git diff” command and provide the branch names separated by dots. Using this command, Git will compare the tip of both branches (also called the HEAD) and display a “diff” recap that you can use to see modifications.


1 Answers

One possibility:

$ git checkout $(git rev-parse A)
$ git merge B
$ git checkout - # returns you to wherever you were before

This checks out the underlying revision (leaving you in a detached HEAD state) and makes changes from there. The benefit of this approach would be that you no longer need to create and delete a branch. This doesn't make the workflow much shorter but you should be able to wrap this in a function, take a few arguments, and reuse it.

As for your second question: you should be able to use merge-base to determine the common ancestor(s) and use that to either merge or cherry-pick commits (thinking back to having a function that does this work for you).

like image 118
Whymarrh Avatar answered Sep 27 '22 19:09

Whymarrh