Back to Git & Dev Tools
2026-03-157 min read

git rebase (Git & Dev Tools)

Learn git rebase (Git & Dev Tools) step by step with clear examples and exercises.

Why This Matters

In this lesson, we'll delve into understanding and mastering git rebase, a powerful feature of Git that enhances collaborative development and keeps your codebase clean and organized.

Why This Matters

In the world of software development, collaboration is essential. With multiple developers working on the same project, it becomes crucial to maintain a clean and coherent codebase. git rebase offers several benefits that help achieve this:

  1. Keeping your feature branch up-to-date with the latest changes in the main branch.
  2. Squashing or editing commit messages for better readability and consistency.
  3. Linearizing the project history, making it easier to review and understand.
  4. Resolving merge conflicts more efficiently by working on a single branch.
  5. Preparing for cleaner merges when you're ready to contribute your changes back to the main branch.

Prerequisites

To fully appreciate git rebase, it is essential to have a good understanding of Git basics, including:

  1. Initializing and cloning repositories.
  2. Adding, committing, and pushing files.
  3. Creating and switching branches.
  4. Merging branches.
  5. Resolving merge conflicts.

Core Concept

Basic Usage

git rebase is used to integrate changes from one branch into another by playing your local commits forward, applying each commit on top of the latest commit in the target branch. To start a rebase, you first need to ensure that you are on a feature branch and have up-to-date changes:

$ git checkout my-feature-branch
$ git pull --rebase origin my-feature-branch

This command performs an interactive rebase, allowing you to squash, edit, or delete commits as needed. You'll be presented with a series of prompts, each corresponding to a commit in your feature branch:

  1. pick - Continue with this commit (default).
  2. reword - Edit the commit message for this commit.
  3. edit - Open the file changes for this commit for editing.
  4. squash and - Combine this and the next commit into a single commit.
  5. fixup - Combine this commit with the previous one, discarding the current commit message.
  6. drop - Remove this commit entirely.
  7. continue - Skip to the next commit without making any changes.

After you've made your selections, Git will apply the commits in the order specified and create a new, cleaner history for your feature branch.

Common Mistakes

  1. Forgetting to pull before rebasing: Always ensure that your feature branch is up-to-date with the latest changes from the main branch before starting a rebase.
  2. Not resolving conflicts during the rebase: If Git encounters merge conflicts, you'll need to resolve them before continuing the rebase.
  3. Rebasing on an outdated main branch: If someone else has pushed changes to the main branch since you created your feature branch, rebasing against that outdated main branch could lead to issues. Always pull and rebase against the latest main branch.
  4. Not committing intermediate changes during the rebase: If you're editing or squashing commits, make sure to commit any intermediate changes before exiting the editor.
  5. Not using interactive rebasing for complex scenarios: For more advanced scenarios, such as merging multiple feature branches or resolving complicated merge conflicts, consider using an interactive rebase with the --interactive flag.

Worked Example

Let's walk through a simple example of using git rebase. In this scenario, we have a main branch and a feature branch that contains three commits:

$ git log --oneline
1234567 (HEAD -> my-feature-branch) Add final feature
abcdefg Feature implementation part 2
1098765 Feature implementation part 1

Assuming that the main branch has been updated with new changes, we'll start a rebase:

$ git checkout my-feature-branch
$ git pull --rebase origin my-feature-branch
First, rewinding head to replay your work on top of it...
Applying: Feature implementation part 1
Applying: Feature implementation part 2
Applying: Add final feature

In this example, Git will present us with an interactive rebase prompt for each commit. We'll squash the first two commits into a single one and continue with the third:

If you wish to amend all commits in the rebase, you may do so with `git rebase --continue`.
If you wish to edit the commit message for any commit, you may do so with `git commit --amend`.

1. Feature implementation part 1 (pick|reword|edit|squash|fixup|drop)
2. Feature implementation part 2 (pick|reword|edit|squash|fixup|drop)
3. Add final feature (pick|reword|edit|squash|fixup|drop)

Select an action for the first commit in the rebase sequence:

We choose to squash the first two commits and continue with the third:

1. squash 1,2
2. edit 1
3. edit 2
4. continue

Select an action for the first commit in the rebase sequence:

After making our selection, Git will open a text editor with the combined commit message for the first two commits. We'll save and exit the editor to complete the rebase:

[detached HEAD 8765432] Combined feature implementation part 1 and part 2
Date: Thu Jan 20 14:30:00 2022 +0530

Signed-off-by: Your Name <your.email@example.com>

Finally, we'll continue the rebase and complete the process:

After staging your changes and saving to a new commit, use `git rebase --continue` or simply type `continue`. When you are done with the rebase, use `git rebase --complete`.
If you would prefer to skip this particular commit and return to the rebase menu, use `git rebase --skip`. To abort and go back to a clean state, use `git rebase --abort`.

(all): continue
(all): skip
(all): abort

Select an action for the first commit in the rebase sequence:

We'll choose to continue the rebase, which will apply the final commit and create a cleaner history for our feature branch.

Common Mistakes

  1. Not committing intermediate changes during the rebase (continued): If you make changes during an interactive rebase but forget to commit them before exiting the editor, those changes will be lost.
  2. Not using --continue after squashing or editing commits: After making changes during an interactive rebase, use git rebase --continue to proceed with the next commit.
  3. Not cleaning up detached HEAD: If you end up in a detached HEAD state during a rebase, use git rebase --continue or git rebase --skip to clean it up.
  4. Ignoring merge conflicts during the rebase: Always resolve any merge conflicts that arise during a rebase before continuing.
  5. Not understanding the impact of squashing commits: Squashing commits can make it difficult to trace the history of your changes, so use this feature sparingly and with caution.

Practice Questions

  1. You are working on a feature branch and want to update it with the latest changes from the main branch using git rebase. What command should you run?
  2. During an interactive rebase, you've made changes to a commit but forgot to commit them before exiting the editor. How can you save your changes and continue the rebase?
  3. You are working on a feature branch and have multiple commits that need to be squashed into one. What command should you use during an interactive rebase to accomplish this?
  4. You encounter a merge conflict during a rebase and are unable to resolve it. What options do you have to continue the rebase process?
  5. After completing a rebase, you find that you've lost some of your changes. How can you recover those lost changes?

FAQ

  1. What is the difference between git merge and git rebase?

git merge combines two branches by creating a new commit that merges the changes from both branches, while git rebase integrates your local commits into the target branch by playing them forward on top of the latest commit in the target branch.

  1. Can I use git rebase to rewrite the history of my repository?

Yes, but be cautious when using git rebase to rewrite the history of your repository. It's essential to preserve the original commit history for accountability and traceability.

  1. Is it possible to undo a rebase?

Yes, you can use the git rebase --abort command to abort a rebase and return to the state before starting the rebase process.

  1. Can I rebase multiple feature branches at once?

No, you should not rebase multiple feature branches simultaneously. Each branch should be rebased separately to maintain the integrity of your codebase.

  1. What happens if someone else pushes changes to the main branch while I'm rebasing my feature branch?

If someone else pushes changes to the main branch while you're rebasing your feature branch, you may encounter merge conflicts when you try to complete the rebase. You'll need to resolve those conflicts before continuing the rebase process.

git rebase (Git & Dev Tools) | Git & Dev Tools | XQA Learn