Include (realign) some branch changes into other branch

123 views Asked by At

I have this branch situation in my GIT project:

enter image description here

  • Blue is the my principal branch
  • Red and Yellow are branches where I'm writing tho different features

Red and Yellow are not completed and I would like integrate the Blue into Red and Yellow without close them. In this mode I can continue to work in Red/Yellow updated branch and when I will close them I will find less conflict.

How can I do this? rebase ? merge?

2

There are 2 answers

2
axiac On BEST ANSWER

git merge does exactly what you need.

You didn't provide the names of your branches. I'll assume their colors are their names.

git checkout red
git merge blue

makes the red branch current then brings into it the changes introduced by the three blue commits since the red branch was created.

Similar,

git checkout yellow
git merge blue

brings into the yellow branch the same three commits; notice the blue branch was merged in the yellow branch before and only these three commits since the last merge are missing from the yellow branch.

If you follow the yellow branch backwards, its last two commits are merges from the green branch (created using git merge green) and the third commit is a merge from the blue branch (created using git merge blue).

git rebase does a different thing, it is not useful for your goal. It can be used when you have a feature branch (the red and yellow branches in your scenario) and you want to move the commits it contains on top of the main (blue) branch, as if they were created after the current state of the blue branch.

0
Makoto On

Take your pick:

  • git-merge will create history which indicates that the two branches you had have become one, unless it would be a fast-forward merge. This is the standard operation for most people but it introduces useless merge commits, as the fact that the mainline branch was merged into the feature branch is meaningless (usually the other way 'round is significant).

    You'd invoke this with git merge <mainline> while on whichever branch you want.

  • git-rebase will accomplish the same thing as the merge, with the added bonus of rewriting your history such that it appears that your commits appear ancestrally after any work that happened on the mainline branch. This means that the history is a lot cleaner and more linear. The caveat to this is that you are rewriting history, and if there is anyone else that depends on the state of those branches' history, then rebasing is a less savory option.

    You'd invoke this with git rebase <mainline> while on whichever branch you want.

If you want to experiment with it a little bit, you can use this web-based tool which can replicate much of what you'd see in the commit history after performing these actions.