# Step-by-Step Guide to Git Interactive Rebase for Beginners

If you are like me and just at the start of your coding journey, `git rebase --interactive` might be a scary command. I feared that if anything went wrong I would lose a lot of my code and have to do that work all over again.

But that was the point of using a version control system like git, **to not worry about changing your code**. Git is here to save our code not to destroy it.

That said Git Interactive Rebase is a powerful command, if not used right it can cause some problems you wish you didn't have.

Although, most such problems can be fixed by `git rebase --abort`, frankly, it's my favorite git-rebase command. XD

This blog post is not about what git rebase is, what are its use cases, etc. This article will focus on successfully using interactive rebase when you need to. I recently had to do it and it was my first time rebasing in such a large codebase, it was quite intimidating, so I'm writing this blog post. To help my future self and others in the same situation.

## When to use interactive rebase

When you want to

* change the order of commits
    
* squash commits
    
* delete (drop/remove) commits
    
* edit files in commits
    
* fixup
    
* exec
    

but we are just going to cover

* changing the order of commits
    
* squashing commits
    

## How to use interactive rebase

Let's assume the following git history:

![1st](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/1.png align="left")

let's visualize this the `git log --oneline --decorate --graph` command

![2nd](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/2.png align="left")

Here, **1.1**, **1.2** are commits on the `main` branch and **2.1**, **2.3**, **2.2** are commits on the `feature-2` branch.

### Issues to fix

1. Here the commit messages on the `feature-2` branch are out of order `2.1`, `2.2`, `2.3` is the correct order of the commits.
    
2. We want to squash `2.3` and `2.2` commits together with the commit message `2.2`
    

### Let's fix the order of the commits

We can use the following command to rebase the last 3 commits interactively

```bash
git rebase --interactive HEAD~3
```

alternatively, use `-i` flag

```bash
git rebase -i HEAD~3
```

here, `HEAD~3` means the last 3 commits.

The above command will open your default editor, usually `vi` by default with a file like in the image below.

![3rd](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/3.png align="left")

> **Note**: Here the commits are in chronological order from top to bottom (latest at the bottom). unlike the `git log` command where the commits are in descending order (latest first).

As described in this file we can edit the word to the left of the commit hashes, which is `pick` by default to other words like `squash` or `reword` to perform specific operations on a commit.

But we want to fix the order of the commits in this rebase.

We can swap the positions of the commits `2.2` & `2.3`, that is swap line no. 2 & 3, which will look something like this,

![4th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/4.png align="left")

Now, save and quit the file, if you are in Vim press the following keys `escape` key, `:wq` then `enter` key.

You'll get a message like the following,

![5th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/5.png align="left")

Now our git history looks like the following diagram

![6th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/6.png align="left")

![7th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/7.png align="left")

Now our first issue is resolved using git interactive rebase! 🥳

### Let's Squash Commits

We can use the same command as before to start another interactive rebase on the last 3 commits

```bash
git rebase -i HEAD~3
```

We again get a similar file with the commits in the following order

![4th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/4.png align="left")

now we want to squash `2.3` into `2.2`, for this we will edit the word before the commit `2.3` from `pick` to `s` or `squash`

![8th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/8.png align="left")

Save and quit the file.

Git will open another file similar to the following

![9th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/9.png align="left")

You have to decide which commit message to keep the first or the second. For this demo, we will keep the commit message `2.2`. So we can either delete the `2.2` or add a **#** before it to ignore that message, like so

![10th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/10.png align="left")

Save and quit the file.

Now our git history looks like the following diagram

![11th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/11.png align="left")

![12th](https://blog.chaitanyashahare.com/images/posts/git-interactive-rebase/12.png align="left")

You've successfully squashed a commit using git interactive rebase! 🥳

## Conclusion

Git Interactive Rebase is a helpful tool that should be in the pocket of every software engineer. It gives us a way to edit the git history. Remember to only rebase personal or unpublished branches, we should never change history of a public branch on which other people might be working on, as a rebase changes the hashes and they are essentially new commits with the information from the old commits.

I hope you found this article helpful.
