Git merge vs Git rebase

Do you know the difference between git merge and git rebase?

I’m sure you do. But do you know when it’s better to use each other?

Git merge creates a new commit on top of two branches.

Git rebase moves a branch on top of another branch.

When it’s better to use one or another?

Git merge shows the development history: somebody created a separate branch and merged it into the main branch.

Git rebase keeps a clean and linear history: all the commits are aligned, no arrows crossing all along the history.

But…

When updating a feature branch with the main branch…

If I use a git rebase, I will keep a clean history of my feature branch. Still, if my feature branch has too much commits, the rebase will be long and complicated.

If I use a git merge, the conflicts are only shown once, as there is only one new commit. Still, the history of my feature branch will be messy. I will see arrows going from the main branch to my feature branch and vice-versa.

So, I use git rebase to update my feature branch when I have one commit on my branch or a few commits. And I use git merge when I have plenty of commits in my feature branch.

I explain this and more in my new ebook, check it here: https://sergiolema.dev/git-book/

My New ebook, How to Master Git With 20 Commands, is available now.

Leave a comment

A WordPress.com Website.