Hey there! I’m Srebalaji. You are receiving this email because you have subscribed to level up your game in Git.
While merging the branch or taking a pull, Git tries to merge using many strategies whichever is suitable.
In this article, we are going to see two strategies. How it is working and when to use it:
3-way merge
fast-forward
Three-way merge
This strategy is used by Git when there are two branches involved and they have diverged.
Let me give you an example,
Assume you have a master branch. Now you have taken a develop branch from that.
And now you start to work on that branch. Now while you are working on it, other team members have pushed their changes on master.
Now you have some commits in your branch develop and some changes are present in the master after you have taken the branch.
Now if you try to merge develop to master, Git will try to use this strategy and merge it.
Three-way merge as the name suggests gets the commits from the two branches and the common commit between the two branches and then tries to merge it.
Three-way merge always creates a merge commit. So the merge commit will have two parent commits.
Fast forward merge
This strategy is used by Git when we got two branches and the target branch doesn’t have any new changes
Let’s see with an example,
You have a main branch. You took a branch develop from the main branch.
You have committed some changes in your branch and meanwhile, there are no new changes in the master.
Now you try to merge develop to the main branch, Git uses the fast-forward merge strategy to merge the branch.
In this case, there is no new merge commit is created. It can be understood that Git copies all the commits from develop and pastes them to the main branch.
In the background, how Git achieves this by fast-forwarding the develop branch pointer to the main branch pointer.
So in Git, branches are just a pointer in the commit. So in this case, Git just changes the pointer from the develop pointer (commit) to the main pointer (commit)
When to use which strategy
A three-way merge strategy can be used when you need a merge commit. If you are working on a big feature release and you need a specific commit in the history which describes that the particular feature is merged.
If the branch has already diverged, you don’t need to do anything. Git will automatically use this strategy to merge.
If your branch has not diverged, then you can tell Git to use this specific strategygit merge main --no-ff
Use fast-forward merge when you don’t need a merge commit. Let’s say you are merging a few commits into your feature branch and a merge commit doesn’t add much value there.
If the target branch has not diverged, then Git will automatically use this strategy to merge. But if you need Git to use this particular strategy:git merge main --ff
This command tries to merge with fast forward. If not possible then it uses a three-way merge strategy to merge and creates a merge commit.
But if you need Git to use only this particular strategy, then you can usegit merge main --ff-only
This command will use only fast-forward approach and if it can’t then it fails with an error.
If you need to fast-forward approach and your branches have diverged. Then you can rebase your branch with main and then use this strategygit rebase main
git merge main --ff-only
Merge strategies in Github
You can see there are three strategies
Create a merge commit - This is similar to a three-way merge
Squash and merge - In this case, all the commits in the branch are made to a single commit and that commit is merged without any merge commit. GitHub will give you the option to type the commit message. This will be helpful if you don’t want a merge commit and as well don’t want to pollute the history with too many commits
Rebase and merge - As the name suggest, this will first rebase with the main and then merge and there will be no merge commit
That’s it for today, folks :)
If you like this article please share it with others.
See you next week :)