Merge branches in Git

Merge branches in Git

Few days ago, my superior gave a task to make a presentation about the daily usage of git for R&D department, so I resumed the git pro book to prepare this presentation. And the following article is to recognize my former wrong and unclear opinion about branch merging in git, and a little suggestion about branch management.

So how to merge two branches?

There are two methods to merge two branches, merge and rebase. Merge method is to merge two branch and generate a new commitment, whose parents are the two HEAD of the older branch. This is called a cross in the branch network. While the rebase method is to re-play every commitment of a branch to another branch, and there are no cross between them after two branch's rebase.

And the best way to check the branches is working in right way is to check if there are crosses in branches. So the recommended method of combine two branches is to always use rebase instead of the merge. Because If you always use rebase to combine two branches, there should not have any cross between branches. Then what's is the right way to use rebase.

The right way to use rebase is to always remember to rebase master branch in working branch.

Following is the usual scenario of merge two branches to master branch. The team leader build two branches named mercury and mars to two programmers for different jobs. After the programmers finished theirs jobs and asked for merge request, he can operate in following steps.
  • rebase branch mercury
# rebase mercury branch to master first
git checkout mercury
git pull
git rebase master
git checkout master
git rebase mercury
  • rebase branch mars
# rebase branch mars, and it should have some conflicts
git checkout mars
git pull
git rebase master
# file1 conflicts , resolve the conflict, and add the file1
git add ./file1
git rebase --continue
# resolve the conflict if there are more
git checkout master
git rebase mars
  • inform the team mates the merge request is finished. And don't forgot the build new branches for every team mate. The former branch mercury and mars should be deprecated.

Comments

Popular posts from this blog

Bluedroid stack in android

How to setup a NAT server?

Network programming in elisp