Understanding Git Rebase
Tags: git, beginners
Published: 2022-11-25 17:21:17

Understanding git rebase is confusing when you start working with it. Here's how I understand Git Rebase.

### What is Rebase
>Git Merge and Git Rebase does the same thing - both can pull changes from other branches. But Rebase works little different from Merge.

### ELI5 version

>#### Git Rebase
>You are about to travel for few days so you start packing bags. You put your t-shirts, charger, headphones, toothbrush etc. Your friend is about to come at your place to drop you at railway station. Your friend bought a gift for you - a new shiny jeans. You want to put the jeans below all of your cloths therefore you pull all your cloths from bag/suitcase, put jeans and put back your cloths. This is basically rebase.
- items in bag is your feature branch
- new blue jeans is your master branch
- when you reshuffle cloths to put new jeans below other cloths is git rebase

>#### Git Rebase Vs Git Merge
- in `git rebase` you add more cloths to your base as you receive them. so it is properly organized and easy to pull cloths, easy to manage.
- in `git merge` you are lazy - you put that jeans and any other item on top of your bag as you receive them. This is hard to pull specific things when you need them, results merge conflicts often complex one and overall hard to manage.


### How Rebase works
>Let's say you create a new branch `feature/contact-us-page` from `master` branch. While you are working on your feature the master branch gets updated with new features.

>Consider following history of feature and master branch

>| feature/contact-us-page| master | 
| ------------- |:-------------:|
| commit 102      | commit 106 Merge branch home-page |
| commit 101 | commit 105 Merge branch about-us-page |
| commit 100 | commit 104 Merge branch login-page |

>When you run `git rebase master` on your branch, it pulls changes from master and move them below your current branch changes. Here's how your feature branch commit will look

>| feature/contact-us-page| master | 
| ------------- |:-------------:|
| commit 102 | commit 106 Merge branch home-page |
| commit 101 | commit 105 Merge branch about-us-page |
| commit 100 | commit 104 Merge branch login-page |
| commit 106 Merge branch home-page      | |
| commit 105 Merge branch about-us-page      | |
| commit 104 Merge branch login-page | |

>Master branch history when you merge your branch into master using merge strategy

>| master | 
| -------------:|
| commit 102 |
| commit 101 |
| commit 100 |
| commit 106 Merge branch home-page |
| commit 105 Merge branch about-us-page |
| commit 104 Merge branch login-page|

### VS how Merge works
 
>When you run `git merge master` on your branch, it pulls changes from master and move them into a new commit. This new commit is on top of your changes. Here's how your feature branch commit will look after git merge -

>| feature/contact-us-page| master | 
| ------------- |:-------------:|
| commit 107 Merge branch master | |
| commit 102 | commit 106 Merge branch home-page |
| commit 101 | commit 105 Merge branch about-us-page |
| commit 100 | commit 104 Merge branch login-page |

>Important thing to note here is `commit 106 feature/home-page`, `commit 105 feature/about-us-page` and `commit 104 feature/login-page` are still present in `commit 107 merge commit`.

>Master branch history when you merge your branch into master using merge strategy

>| master | 
| -------------:|
| commit 107 Merge branch master |
| commit 106 Merge branch home-page |
| commit 105 Merge branch about-us-page |
| commit 104 Merge branch login-page |

### Rules for Rebasing

>- When not to do Rebase - Always rebase on your own branch. Never use it on public branches
>- Force Push with Caution - When you rebase, git pulls latest changes and move it below your changes. It basically rewrites your history. To push changes to origin you need to do force push - `git push --force`. **Please be very careful**, if you push on some other branch, it will rewrite history of that branch and there is no way to revert it.

### Benefits of Rebasing over Merge

>- You will get much cleaner history
>- Reverting changes or whole branch is a lot easier because you are always dealing with simple commits. In Merge you deal with merge commits which has other commits.
>- You won't get bigger merge conflicts

---

#### Finally, thank you for reading. I hope it helps you with Rebase.