Welcome to pay attention to the rich way Web development team, lack of conformity

Last week I spent time sorting out the categories, articles, and entry of the Futu Web blog, mainly to solve SEO problems. Next SEO is good or not depends on the effect.

Futu Web blog address

What about today’s article? Xiaobian think we look at the picture. Git branch merge rebase Git branch merge rebase Git branch merge

So before the start of the body, small make up or send some eggs. It is useful to understand Git commands.

File state transition:

File storage:

The above two diagrams will help you understand Git commands.

The body of the

In projects that use Git for version management, there are two ways to complete a feature development and merge it into the Master branch: Git Merge and Git rebase. Git merge is often used more than git rebase, but git rebase is also extremely powerful. Git rebase git rebase git rebase git rebase

The preparatory work

To get a better look at what happens after git merge and Git rebase, let’s start by creating a project repository, then building two branches in it, and adding a few commits.

Details are as follows:

  1. Create a directorygitTest
  2. In the directorymyTestCreate a new file inreadme.mdAdd a random title and the first list itemadd master1And submit to local warehouse
  3. Create a branch named after the current branchfeature, will list itemsadd master1Modified toadd feature1And submit to local warehouse
  4. Then switch to the branchmasterAdd a new list item to theadd master2And submit to local warehouse
  5. Then switch to the branchfeatureAdd a new list item to theadd feature2And submit to local warehouse
  6. Repeat steps 4 and 5 above until themasterfeatureFour list items have been added to each

The record submitted by your repository branch should look something like this:

The initial master branch should look like this:

The initial feature branch should look like this:

git merge

Make a copy from the directory gitTest and name it gitMerge to perform our merge.

Git merge Git merge git merge git merge git merge git merge git merge

  • Switch the branch tomasterUp:git checkout master
  • The branchfeatureMerge into the current branch (i.emasterBranch) on:git merge feature

As shown in the figure above, Git Merge has the following features:

  • Only one conflict is handled
  • Introduces the history of a merge, all after the mergecommitIt will be sorted from old to new by submission time
  • All processes have more information, which may make it harder to find problems later

Git merge commits too much information, which may affect the difficulty of finding problems. Because in a large project, relying on the git merge method to merge, will save all of the submission process information: lead to branch and merge branches, a new branch again on branch, and so on, operating more than one, like this submission history information is mixed and disorderly, at this time if there is a problem need to look up will be more difficult.

Git merge branch commit logs are as follows:

git rebase

Git rebase merge

Like Git merge, git rebase is used to merge changes made in one branch into another branch.

As the picture above shows, his main characteristics are as follows:

  • Change the current branch frommasterThe location of the pull-up branch
  • There is no redundant record of merge history, and after mergecommitNot necessarily in ordercommitOf the submission time
  • May resolve conflicts in the same place more than once (yessquashTo solve)
  • A little more refreshing,masterEach of the branchescommitPoints are relatively independent and complete functional units

Git rebase: Git rebase: git rebase

Git rebase master = git rebase master = git rebase master

Git rebase –continue — git rebase — git rebase –continue — Git rebase — git rebase — git rebase — git rebase — git rebase — git rebase — git rebase

Git rebase –continue: Git rebase –continue: git rebase –continue: Git rebase –continue: Git rebase –continue: Git rebase –continue After execution, there was another conflict. This time, it was compared with the second submission of feature branch, which meant that we needed to solve the conflict in the same place for many times.

Continue the process of resolving the conflict and git rebase –continue until you encounter:

It means that the base-changing operation of the feature submitted for the last time is completed, so the whole base-changing operation is completed.

Git rebase git rebase

It completely conforms to the characteristics of git rebase implementation mentioned above. The location of the feature branch we extracted has changed, there is no redundant submission history, and the timing of submission has also changed. In addition, it will be recalled that conflicts in the same place were solved many times during the process of base change.

At this time, we switch to the master branch and merge the feature branch.

It can be seen that all the submitted information on the feature branch will be merged into the master branch. Such information is not necessary for us. We usually only need to know what new features are merged into the Masetr branch. This extra information can be consolidated using git Rebase’s interactive mode, which is covered in the next section.

Git Rebase interactive mode

To turn on the rebase interaction mode, you just need to pass in the -i parameter and specify which commits to process. For example:

git rebase -i HEAD~4
Copy the code

The above command specifies the last four commits for the current branch. Now we use the command above to merge the submissions of feature branches.

There are some commands in the red box in the middle that can be used to process a commit. Here we use squash to merge all commits into a commit. After editing and saving the commit, there will be a prompt for editing the commit.

If you look at the submission record on the feature branch, you will see that there is only one submission.

Whether the branch is merged directly into the master or rebase and then merge, the commit record on the Master branch will be very clean.

conclusion

Git merge Git merge git merge Git merge Git merge Git merge Git merge Git merge git merge git merge Try Git rebase if you find yourself making frequent Git commit attempts while modifying a feature and find that too much commit information is not necessary.