TLDR
Both git rebase
and git merge
are used to synchronise changes between branches. The difference is git rebase
will revise the commits but keep a clean organised history, while git merge
will keep all the original commits and add ones automatically but gives out a circuit board like history.
Real world scenarios
Image you have two branches: feature
and master
. feature
is from master
, after some time, for some reason, master
has some changes which feature
doesn't have.
Git merge
What git merge do:
- Wouldn't change the commit timestamp
- Wouldn't change the commit hash
- Will create a new commit automatically
- When there are many people are using the git merge in a project, it will cause commits across each other in history, in the end it will show the commits like a Circuit board.
How to do git merge:
- Step 1:
git checkout feature
. For example,git checkout feature/new_awesome_feature1
. - Step 2:
git merge master
, might need to resolve some conflicts while doing this step.
Git Rebase
What git rebase do:
- Change the commit timestamp
- Change the commit hash
- Not introduce any new commit automatically
- Generate nice and organised git history, like the one in the following:
git rebase will move the current changes to upmost, and keep the current change history.
How to do git rebase:
- Step 1:
git checkout feature
- Step 2:
git rebase master
, might need to resolve some conflicts while doing this step. Here we might rebase the code multiple times. So it is a good idea to squash all the commits into one.