Git Merge vs Rebase: The Three Types of Merge
Merging is adding the work done in one branch to another. There are three ways one could merge one branch into another:
I would call the 3rd option "real merging" to emphasize it when necessary.
TL;DR
This piece subtitled "The Three Types of Merge" focuses mainly on setting up the ground for the discussion on whether to use rebase or merge. A follow-up article called "Git Merge vs Rebase and Where to Use Them" will cover the pros and cons of each workflow.
Fast-Forward Merge
When you have only new commits in the source branch, fast-forward merging is simply adding those commits to the destination branch. Easy!
Rebase and Fast-Forward Merge
The fast-forward merge is only possible if the target branch is an ancestor of the source branch, which is usually not the case. You have added 2 commits to your feature branch and by the time you want to merge it back to master, your colleagues have added 4 commits to it. In this situation, we say the feature branch is 2 commits ahead of the master and 4 commits behind.
One cannot perform a fast-forward merge if the feature branch is anything behind. To solve this problem, we do something called a "rebase". By rebasing the feature branch onto master, we remove the newly added branch commits, update the branch with the current state of the master, and then add the remove commit on the tip of the master.
git switch feature_branch
git rebase master
After performing the rebase, the feature branch becomes 2 commits ahead and 0 commits behind the master, and hence we can perform the fast-forward merge.
git switch master
git merge --ff-only feature_branch
领英推荐
Real Merge
Let's return to the case with the feature branch 2 commits ahead and 4 commits behind master. When performing a real merge, a new commit is added to the master branch that contains all of the new changes from the feature branch.
git switch master
git merge feature_branch
The newly added commit is called "the merge commit". It has references to the head of the feature branch, as well as a reference to its previous commit in the master branch; this is why we say the merge commit has two "parents".
Merge vs Rebase
When we talk about "merge vs rebase", we are comparing the following two workflows for merging two branches:
There are a few differences between the two workflows:
Squash Merge
The third type of merging exists which is not getting as much attention. The squash merge basically squashes all of the commits on the feature branch (i.e. packs them all into one commit), and adds the one commit to the target branch.
The main difference is that the newly created commit has no reference back to the branch it was created from. So, this way of merging has similarities to the other two.
Future Work and Conclusion
In a the next article in the Git Weekly series, a full comparison between the workflows will be carried out. This article was originally published on Medium. I write weekly on git and monthly on Docker:
Rechtsanwalt | Gesch?ftsführer und Softwareentwickler bei Maramia GmbH i.G.
1 个月I learned something at your recent talk JobRad | Deutschland Thanks, mate!