Two methods of GIT branching and merging

Time:2020-11-23

How to merge two branches together. That is to say, we create a new branch, develop a new function on it, and then merge it back to the main line after the development.

1.   git merge

Let’s take a look at the first method——git merge

Merging two branches in Git results in a special commit record that has two parents. Translation into natural language is equivalent to: “I want to include these two parent nodes themselves and all their ancestors.” This is explained in detail below.

#Create a new branch bugfix
git branch bugFix

#Switch to the branch
git checkout bugFix

#Submit once
git commit

#Switch back to master
git checkout master

#Submit again
git commit

#Merge bugfix into master with git merge
git merge bugFix

(the branch of the master is mixed with all the submission records, but bugfix does not, so the following operation should be performed)

#Then merge the master branch to bugfix (because the master inherits from bugfix, GIT does nothing but simply move the bugfix to the submission record pointed by the master.)
git checkout bugFix;
git merge master

2.  git rebase

The second way to merge branches is togit rebase。 Rebase is actually taking a series of submission records, “copying” them, and then placing them one by one in another place.

Rebase’s advantage is that it can create a more linear submission history, which sounds a little difficult to understand. If only rebase is allowed, the commit history of the code base will become extremely clear.

#Create a new branch, bugfix branch
git branch bugFix

#Commit once (commit on master branch)
git commit

#Switch to the bugfix branch
git checkout bugFix

#Commit once (commit on the bugfix branch)
git commit

#Use git rebase to move the work in the bugfix branch directly to the master branch (after moving, the functions of the two branches will appear to be developed in sequence, but in fact, they are developed in parallel).
git rebase master

Note: the commit record C3 still exists (the translucent node in the tree), and C3 'is a copy of C3 on our rebase to master branch.

#Switch to the master branch (to update the master)
git checkout master

#Rebase the master to the bugfix branch (since bugfix inherits from the master, GIT simply moves the reference of the master branch forward.)
git rebase bugFix

This article on the detailed explanation of GIT branch and merge of the two methods of the article introduced here, more relevant git branch and merge content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!