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
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With