Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git - Temporarily merge others branch into my current branch

Tags:

git

git-merge

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 ?

like image 300
Conans Avatar asked Apr 16 '15 16:04

Conans


People also ask

What does merge into current branch do?

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.

Which command is used to merge another branch into the branch where you are currently working?

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.

Does merging affect both branches?

No, merging does only affect one branch.


2 Answers

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?

like image 142
raylu Avatar answered Oct 18 '22 04:10

raylu


There are two valid ways to approach this:

  1. 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.

  2. 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.

like image 1
cmaster - reinstate monica Avatar answered Oct 18 '22 05:10

cmaster - reinstate monica