I have a situation. Let's say two developers are working on two different branches A
and B
.
-- master --
| |
A <-- B
Branch B
is dependent on changes in A
. However, changes in A are not yet merged into master
. I would like to start working on my feature (branch B
) with changes in A
and then discard them when I am done testing. What is the recommended way ?
Merging is Git's way of putting a forked history back together again. The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch. Note that all of the commands presented below merge into the current branch.
The "merge" command is used to integrate changes from another branch. The target of this integration (i.e. the branch that receives changes) is always the currently checked out HEAD branch.
No, merging does only affect one branch.
Let's say branch B is currently on commit abc1234
. The most direct answer to your question is
git checkout B
git merge A
# run your tests
git reset --hard abc1234
But as others have mentioned, this is a really really really odd workflow. Why do you want to unmerge the branches in the first place if B depends on A? Perhaps you want a 3rd "integration" branch instead?
There are two valid ways to approach this:
You do a full, normal merge. This is the easiest approach, git does not care how many times you merge back and forth, it can sort it out. This approach is the most honest one: Your changes depend on what's been done in the other branch, so that's reflected in the recorded history.
The only problem with this approach is people (probably with an SVN or similar background) who want to retain a linear history on master
, or an SVN repository that contains the official development history.
You rebase your work onto the other branch. Some people like this because it retains a linear history, and it makes interacting with an upstream SVN repository easier. The downside is, that you are lying about history (see my answer to another SO question for more details on why that is problematic). So, if you choose to rebase, you should make sure that you at least compile-test your new commits.
Another downside with this approach is, that you will need to rebase your entire work every time you need to draw on changes in another branch (including master) to retain a linear history. And to repeat the testing of all the new commits that the new rebase creates.
That is why I strongly prefer the merge approach: It allows merging at any point, and the only new commit that needs to be tested is the merge commit. All my previous commits remain unchanged and don't need to be retested.
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