In my initial days of git, I was a lot confused about merge and rebase. Though both works, it was not much clear which one to use. This post dives into the basic difference between the two.

Consider the scenerio when you are working on a feature branch. The feature branch has moved some commits ahead, and so is the main branch.

Now we want to get all the extra new commits from the main branch into the feature branch. There are two ways we can do it, we can either merge main branch to our feature branch, or we can rebase our feature branch on the main branch. What is the differenece between the two appraoches

Git Merge

Git Merge takes all the new commits from the main branch, combines it together and applies it as big commit on our feature branch.

Git Rebase

The Git Rebase applies the commits from the feature branch one by one, on the top of the main branch first. Then it makes the feature branch point to the head of this new commits.

The main differene between the two is that how the git commit history is maintained. In the git merge case, all the commits of the merge branch will be present as a single commit in our feature branch. Whereas in the git rebase case all the commits of the main branch are as it is available in our feature branch and it will look like we started working from the HEAD of the main branch.

References and Image Credit