An Introduction to Git Rebase: A Tutorial
Tags: tutorial, beginners, webdev, github
Published: 2021-06-14 13:41:36

A month ago, I posted on [twitter](https://twitter.com/zahrakhadijha) that I was learning `git rebase` at work and how scary it felt.

{% twitter 1393292478393032714 %}

It got a lot of engagement from people feeling similarly about rebasing and some in the same boat as me where they felt that they didn't understand it. 

A month later, practicing, watching A LOT of videos on it and rebasing almost every day, it's not so scary anymore. So I wanted to write up a tutorial on the best way I understand it in hopes that it helps you, too. 

## Git Rebase

The first thing to understand about `git rebase` is that it's designed to integrate changes from one branch into another branch. Rebasing will take all of the commits you've made on your feature branch and move them on top of the master commits that other team members have made *while* you were working in your branch. As a result, you'll have a nice clean tree, like so in the image, where `my_awesome_feature_branch` is your branch after rebasing with the `master branch`.

![A Clean Tree](https://i.imgur.com/z6IseHF.png)

## Here's HOW to Rebase

Let's say that that you got assigned to work on a really awesome feature on a massive project. This project is called **The Really Cool Project**. The first thing you'll always want to do is make sure that the `local master` is in sync with your `remote master` branch.

1. Open up your terminal, go into the project folder, and on your local machine, you'd want to run

```
git pull
```

Your `local master` branch should be up to date with the `remote master`.

![Local and Remote Branches](https://i.imgur.com/TBRWI04.png)

2. Next, you'll want to checkout into a new branch so that you can work on your feature without disrupting the `master` branch. 

Run the following command

```
git checkout -b my_awesome_feature_branch
```

Now you should be in your feature branch, ready to build that awesome feature for your project.

You've decided that you want to add and style the title of this feature. So you make a commit within your branch

```
git add .
git commit -m "feature/add-title"
git push origin my-awesome-feature-branch
```

Your local branch now looks like this

![Title Branch](https://i.imgur.com/fhvahEV.png)

After making your first commit, you continue to work in your feature in your branch, adding more code and making progress on this cool project. You make another commit so your branch now looks like:

![Content](https://i.imgur.com/BntdYmH.png)

While you're working on your awesome feature, the rest of the team is working on some cool features too and they're merging their features to the `remote master` branch. Your teammate built a cool NavBar and finished it in two days, so it got merged into the `remote master`, while your other teammate built a cool Footer feature and it's also merged into the `master` branch. The git tree now looks like

![NavBar](https://i.imgur.com/Cu410j2.png)

The `remote master` is not in sync with your `local master` anymore. And your feature branch is now behind and doesn't have the latest changes made in the development environment. You add another commit to your branch and decide that you're ready to merge your feature with `master`. 

![Cool Image](https://i.imgur.com/xxyj5OY.png)

What you need to do now is `rebase` so that your feature branch is in sync with the `remote master` and that you're up-to-date with the latest changes to the master. This is the flow you'd want to run:

```
git checkout master
git pull
```

I now have all of my teams changes on my local branch

![Git Pull](https://i.imgur.com/B4WKIba.png)

## Time to REBASE!

So now I want to make sure that my feature branch will also be in sync with those changes so I'll check back out to it.

```
git checkout my-awesome-feature-branch
git rebase master
```
![Rebased](https://i.imgur.com/cPwBB0P.png)

What just happened is that your changes have been integrated with the latest `master` branch. Assuming that you don't have any conflicts in your files, you're ready to merge into `master`. If you DO have a conflict in your branch, git will let you know and you'll have to resolve those conflicts. Usually those happen when you and another developer worked on a feature that required you to make changes in the same file. 

![Meme](https://lh3.googleusercontent.com/proxy/LNqtEE0-4Yel67VQBwq8NpRKQHQsPstubGXGGp3IZBYz70b7OopUIMpH0lXy-yMOYTadzqwsrb5DZzCxL4_heBcUp02o5b7VCM-gIpAs5Kmmzw_4frCg8UW5mQEcEU-2SVbxGMibqf1JRoqBKwvNqOvrLJxb8Ilxf-E)

Follow Up Reading:
* [Official Git Documentation](https://git-scm.com/docs/git-rebase)