Git Rebase and Git Merge solve the same problem. Both are merging commits from one branch to another. How are they different?

git merge

The master branch joins the feature branch

git checkout feature
git merge master

/ / or
git merge master feature
Copy the code

Git merge has the advantage of preserving the structure of branches and historical commit directories, but it also leads to a lot of merge contamination of commit history

git rebase

The rebase command is a frequently heard command that most people don’t know very well. Rebase merging is often referred to as “rebase”

It compresses all submissions into a patch. Then add patch to the target branch. Rebase differs from Merge in that Rebase rebase rewrites project history by creating new commits for each commit in the original branch

Rebase feautre on the master branch:

git checout feature
git rebase master
Copy the code

Git Rebase has the advantage of having a clearer project history. First, it eliminates unnecessary merge commits required by Git Merge; Second, as you can see in the figure above, rebase produces a perfectly linear project history that you can trace all the way to the initial commit of the project without any forks on the feature branch.

However, Rebase loses the context of the merge commit, preventing us from seeing when the actual change was merged on the target branch

Interactive Rebase

This command is more powerful than Git rebase in that the commits change when they move to a new branch. Typically, these are used to clean up the history of the particular commits before they merge a feature branch to the master.

git checkout feature
git rebase -i master
Copy the code

This will open a text editor listing all submissions that are about to be moved:

pick 6633b5a Message for commit #1
pick 3a03f0d Message for commit #2
pick 657897b Message for commit #3
Copy the code

It clearly shows what the branch looks like after rebase. By readjusting, the commit history can look anything you want.

Eliminating such meaningless commits makes your feature history easier to understand. This is something that Git Merge simply cannot do.

Commits are currently pick, fixup, and squash. Executing git rebase -i in the git directory indicates that commits are rearranged or consolidated as needed

git merge vs git rebase

Git merge:

  • Record merge actions, which are often spam
  • Will not modify the originalcommit ID
  • Conflicts are resolved only once
  • The branches don’t look neat, but you can see the order of the merge
  • Record the realcommitInformation, including details of each branch

Git rebase:

  • Change the current branchbranch outThe location of the
  • Get a more concise project history
  • eachcommitBoth require conflict resolution
  • Modify allcommit ID

My advice

As the team grows, it is difficult to manage and track every commit through the Merge strategy. Using Rebase is a smart and efficient choice for a clearer and easier to understand submission history.

Here are some suggestions for different environments to get the most out of Rebase:

If you are not working with others, you should use Rebasing instead of merging, so that the historical record is clear. If you have already pulled your personal fork from the repository and are not ready to work with other developers, rebase before the branch push is also ok.

Your code is ready for review: You created the pull Request. Someone else is reviewing your code and may drag it to local review. If so, you’d better not rebase your code. You should create a “rework” commit to update your feature branch. It makes pull requests more malleable and prevents sudden loss of history.

Review is complete and ready to be merged into the target branch. A: congratulations! You need to delete your feature branch. Since other developers don’t need to pull and merge these changes, this is your chance to clean up your records. You can rewrite records, collapsing the original commit, “PR rework” commit, and “merge” commit into one clean commit. Optionally, you can also create an explicit merge for these commits, which is actually useful. It will record the time when the feature is merged into the master.

Reference:

Merging vs. Rebasing

An introduction to Git merge and rebase: what they are, and how to use them

Three minutes a day