What you need to know about git rebase and git merge as a beginner

Emmanuel Onyebueke
3 min readMay 14, 2022

--

I struggled understanding the difference between git rebase and the git merge command, why and when to use which command, after reading some articles and answers on stack overflow, here is what i found.

Git Merge

Git merge adds or integrates your updated changes from feature branches to the base branch. For example, we are creating a project which have a base branch called master and have the following feature branches: add , multiply, divide, substract with different developers working in each of these branches.

Branch list

Now, we are working on the multiply operation, so we git checkout multiply and add our commits.

Latest commit in multiply branch

The recent commit in multiply is add multiply function, we are done with this branch, we have tested the feature and it can be added to the base(master) branch.

To add changes from the multiply to the master locally, we use the merge command. First, let’s checkout into master and view the recent commits.

Latest commit in master branch

The recent commit in master is a made add an higher order function at 09:39AM. Let’s git merge multiply into master (Note: to merge a feature branch to a base branch, you must checkout into the base branch before merging).

Now, our master branch has all the commits in multiply.

Git Rebase

We use git rebase to add base branch changes in the feature branch. So we’ve merged changes from multiply to master ,Meanwhile, another team was working on divide , they are done with the divide operation and have committed their changes.

Latest commit in divide branch

Notice, the divide is not aware that the master has been updated, and to prevent merge conflicts, we update the branch. To do this we use git rebase , rebase goes through the commit history of master and place commits in chronological order.

rebasing master to divide branch

After rebase, the divide branch log is updated with the recent updates in master .

We could also use git merge but it is against best practice, because it creates an extra merge-commit and one could have trouble visualizing the git history.

Summary

We use git merge to update changes from feature branch into base branch, and we use git rebase to add changes from base into feature . Thank you.

References

--

--