5) Git & GitHub Tips & Tricks Lesson

Git Rebase

11 min to complete · By CodingNomads Team

The git rebase command allows you to include the changes from one branch into another branch.

What is Rebasing in Git?

git rebase moves all commits of one branch onto a new base commit. Rebasing in Git, therefore, changes the base of your working branch from one commit to another, making it look like you created your branch at a different point in time.

Git does this by creating new copies of your initial commits, and sticking these copies onto the new base commit you specify. Even though your branch will look like it has the same commits as before, it is actually created from a set of totally new, but same-looking, commits.

Illustration of a lighthouse

Alert: Don't use git rebase on public and shared repositories unless you know what you're doing and what could go wrong.

To dig deeper into the git rebase concept, let's begin by looking at the command you use to make a rebase happen:

$ git rebase <base>

When running this command, your current branch will rebase onto <base>. The <base> will usually be the name of another branch, which means that Git will rebase onto the last commit of that branch. However, <base> could also be a different kind of commit reference, such as a tag or a specific commit hash.

Git Merging vs. Rebasing

The git rebase and git merge commands can both solve the same task: to include changes from one branch into another. However, they accomplish this task through different methods:

The difference between Git merging vs. rebasing is that git merge correctly preserves the history of your project and avoids potential conflicts when working collaboratively, while git rebase makes the individual commit history of a branch cleaner and easier to read.

Let's look at an infographic that describes the differences between the two commands:

Git Merge vs. Git Rebase Comparison

When to Use Git Rebase vs. Merge

Let's assume you're working with a feature branch based on the main branch. Since you started working on your feature code, the main branch has also progressed. Both branches contain commits that the other one doesn't have, and your branch is no longer synced with the remote main branch.

You could git merge the main branch into yours. In a collaborative environment, this is probably a good idea. However, it will introduce a merge commit somewhere between your own commits, and might make your commit history more confusing to understand.

In this scenario it might be better to to use Git rebase vs. merge. With git rebase you can continue having a clean commit history, and also incorporate the changes made to main into your feature branch.

Git Rebase Example

To perform a rebase in the example situation described above, you can follow these steps:

  1. Use the git checkout command to navigate to your main branch and pull the changes from the remote repo to your local main branch
  2. Then git checkout back to your feature branch
  3. Run git rebase main to apply all your commits of the feature branch to the end of the latest change in the main branch

Let's look at this in a graphic:

Git Rebase

As you can see, Git rebasing means changing the base of your branch from one commit to another, and making it appear as if you've created your branch starting from a different commit.

Interactive Rebasing

If you use the -i or --interactive option with the git rebase command, Git will take you to the interactive rebase shell prompt. Like with a non-interactive rebase, you need to pass a branch name or commit hash where you want to rebase on.

The interactive rebase allows you to do several operations like a pick, fixup, rewrite, edit, reorder, and more. If you have a local repo with at least two branches, one of them called main, switch to your other branch and type the following command:

$ git rebase -i main

Git will open up your default text editor to show you a list of all the available interactive options. It looks like in the image below:

Interactively rebasing

If you like working with Git's interactive mode, feel free to go through the different options and familiarize yourself with them.

Considerations Before Rebasing

Merge Conflicts

When you use git rebase to combine two branches that haven't been in sync for a long time, there's a high possibility that merge conflicts will occur. The longer your feature branch has diverged from the main branch, the more potential conflicts could have cropped up. A rebase will likely lead to merge conflicts you'll need to resolve.

Rewriting History

Rewriting History is a more serious situation that can also occur while working with git rebase. If you, for example, execute sub-commands such as squash or drop during an interactive rebase, commits will be removed from a branch's log. This can lead to the commits being permanently gone. However, there might be a way to undo the rebase using Git reflog, which can restore these commits.

Collaboration

The most dangerous situation with using git rebase occurs when you are working on a collaborative project and you push the results of your rebase to a remote branch that is shared with other users. You should avoid doing this as it can overwrite other users' work when they pull remote changes to their local machines.

Colorful illustration of a light bulb

Note: As a rule of thumb, don't rebase public repositories that you work on with other people.

In any case, you should check carefully and make sure you understand what you are doing and what the effects will be before you rebase.

For more information, check the documentation on Git Rebase.

Summary: What is Git Rebase?

  • The git rebase command is a way to include changes from one branch into another branch.
  • Git does this by creating new copies of your initial commits, and sticking these copies onto the new base commit you specify. Even though your branch will look like it has the same commits as before, it is actually created from a set of totally new, but similar, commits.
  • Git merging vs. rebasing: The git rebase command makes the individual commit history of a branch cleaner and easier to read, while git merge correctly preserves the history of your project and avoids potential conflicts when working collaboratively.
  • If you use the -i or --interactive option with the git rebase command, Git will take you to the interactive rebase shell prompt.

Considerations Before Rebasing

  • Merge Conflicts
  • Rewriting History
  • Collaboration