Practical Usages Of Git Rebase and Git Merge

When to use Git Merge and When to use Git Rebase ?

Practical Use cases or examples when Git Merge will be useful and Git rebase will be useful.
Software ProgrammerAsked:
Who is Participating?
 
gelonidaCommented:
In a nutshell:
git merge is mostly used to merge changes from another branch into your working branch.
git rebase  is mostly used to cleanup the history of a branch before you ask somebody else to merge it.

If multiple persons work  on the same branch:
git merge will allow you to merge changes on the same branch done by somebody else
git rebase will do the same, but your git history will be much nicer  to read.


Simple example:

Let's create a simple project with two files and two commits and visualize its history.
mkdir sample_project
cd sample_project
git init # create an empty git repository
touch a b # create two files
git add a b # and add them to git
git commit -a -m "initial commit" # and create a commit
echo a change >> a # change one file
git commit -a -m "a change on the main branch"
git log --graph --oneline --all

Open in new window


Now start your development on a feature with two commits and visualize:
git checkout -b mybranch
echo a change >> b
git commit -a -m "a change on my dev branch"
echo another change >> b
git commit -a -m "a second change on my dev branch"
git log --graph --oneline --all

Open in new window


Your graph would look something like:
* 972755a a change on my dev branch
* f3c7dc2 a change on my dev branch
* a7f417c a change on the main branch
* 3184a78 initial commit

Open in new window

It is a simple linear graph.


However while you were working on your branch things happened on the main branch:
git checkout master
echo a change >> a # change one file
git commit -a -m "meanwhile master changed"
echo a change >> a # change one file
git commit -a -m "master changed again"
git log --graph --oneline --all

Open in new window


Your graph would look now:
* 7be2ff0 master changed again
* 59328e5 meanwhile master changed
| * 972755a a change on my dev branch
| * f3c7dc2 a change on my dev branch
|/  
* a7f417c a change on the main branch
* 3184a78 initial commit

Open in new window


This means you have the main branch and your branch forking off commit a7f417c

If you merged now your branch 'mybranch' into 'master'
git merge mybranch
git log --graph --oneline --all

Open in new window


You would get following version graph.

You see the graph splitting and joining again.

*   ac397d2 Merge branch 'mybranch'
|\  
| * 972755a a change on my dev branch
| * f3c7dc2 a change on my dev branch
* | 7be2ff0 master changed again
* | 59328e5 meanwhile master changed
|/  
* a7f417c a change on the main branch

Open in new window


If many merges happen and especially if man merges happen at the same time, then the version graph will be rather difficult to read.

So the alternative approach would have been to NOT do the merge but use git rebase.
git reset --hard 7be2ff0  # Undo the merge
git checkout mybranch # go to mybranch
git rebase master # and rebase it to master. 
git log --graph --oneline --all # and visualize

Open in new window


Please note, that rebase might (like merge) fail and require manual interaction before you can type git rebase --continue

Now you will have a simple version graph and a merge will be just a fast forward.
* 2a77a20 a change on my dev branch
* f7d72f6 a change on my dev branch
* 7be2ff0 master changed again
* 59328e5 meanwhile master changed
* a7f417c a change on the main branch
* 3184a78 initial commit

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.