codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
📦

Git & GitHub

12 / 63 topics
9Branching and Merging10Resolving Merge Conflicts11Using git stash12Using git rebase
Tutorials/Git & GitHub/Using git rebase
📦Git & GitHub

Using git rebase

Updated 2026-04-20
4 min read

Introduction

git rebase is a powerful command-line tool used in Git for rewriting commit history. It allows you to apply a series of commits on top of another base tip, effectively changing the order and structure of your project's history. This tutorial will guide you through the basics of using git rebase, including its syntax, common use cases, best practices, and real-world examples.

Understanding git rebase

Before diving into how to use git rebase, it's important to understand what it does under the hood. When you run git rebase, Git will take all the changes that were committed on one branch and replay them on another base tip. This can be useful for several reasons:

  • Linear History: It helps maintain a linear project history, making it easier to follow.
  • Conflict Resolution: It allows you to resolve conflicts between branches more cleanly.
  • Squashing Commits: It enables you to combine multiple commits into a single commit.

Basic Syntax

The basic syntax for git rebase is as follows:

git rebase <branch>

This command will take the current branch and replay its commits on top of <branch>. For example, if you are on the feature-branch and want to rebase it onto main, you would run:

git rebase main

Interactive Rebase

Interactive rebase provides more control over how commits are applied. It allows you to edit, squash, or reorder commits before they are applied to the base branch.

To start an interactive rebase, use the -i flag:

git rebase -i <branch>

This will open a text editor with a list of commits that will be rebased. Each commit is prefixed with pick. You can change pick to other commands like squash, reword, or drop.

Example

Suppose you have the following commit history:

commit 3 (HEAD -> feature-branch)
Author: John Doe <john.doe@example.com>
Date:   Mon Jan 1 12:00:00 2024 +0000

    Add new feature

commit 2
Author: Jane Smith <jane.smith@example.com>
Date:   Sun Dec 31 23:59:00 2023 +0000

    Fix bug in existing feature

commit 1 (main)
Author: Alice Johnson <alice.johnson@example.com>
Date:   Sat Dec 30 23:58:00 2023 +0000

    Initial commit

If you want to squash the last two commits into one, you would run:

git rebase -i main

In the editor, change the list to:

pick 1 Initial commit
squash 2 Fix bug in existing feature
squash 3 Add new feature

Save and close the editor. Git will then combine these commits into one.

Advanced Usage

Rebase onto a Specific Commit

You can rebase onto a specific commit rather than a branch by using its hash:

git rebase <commit-hash>

This is useful when you want to apply your changes on top of a particular state of the project.

Abort a Rebase

If you encounter conflicts and decide not to proceed with the rebase, you can abort it:

git rebase --abort

This will return your branch to its original state before the rebase started.

Best Practices

  1. Backup Your Work: Always ensure you have backups of your work before starting a rebase. You can create a new branch as a backup:

    git checkout -b backup-branch
    
  2. Avoid Rebasing Public Branches: Do not rebase branches that are shared with others, as it can cause conflicts and confusion for collaborators.

  3. Use Interactive Rebase Sparingly: While interactive rebase is powerful, use it judiciously to avoid rewriting history excessively.

  4. Resolve Conflicts Promptly: If conflicts arise during a rebase, resolve them promptly to prevent the rebase from becoming too complex.

  5. Test After Rebasing: Always test your code after a rebase to ensure that everything still works as expected.

Real-World Example

Let's walk through a real-world example of using git rebase.

Scenario

You are working on a feature branch and want to integrate the latest changes from the main branch while also squashing some commits for cleaner history.

  1. Switch to Your Feature Branch:

    git checkout feature-branch
    
  2. Fetch Latest Changes from Main:

    git fetch origin main
    
  3. Start Interactive Rebase onto Main:

    git rebase -i origin/main
    
  4. Edit the Commit List: Suppose your commit list looks like this:

    pick 1 Initial feature implementation
    squash 2 Fix minor bug
    squash 3 Add unit tests
    

    Save and close the editor.

  5. Resolve Conflicts (if any): If conflicts arise, resolve them and continue the rebase:

    git add <resolved-files>
    git rebase --continue
    
  6. Test Your Changes: Ensure everything works as expected after the rebase.

  7. Force Push to Remote (if necessary): Since you have rewritten history, you may need to force push your changes:

    git push origin feature-branch --force
    

Conclusion

git rebase is a versatile tool that can help you manage and clean up your commit history. By understanding its syntax, common use cases, and best practices, you can effectively use git rebase to maintain a clean and linear project history. Always remember to back up your work and test thoroughly after rebasing to ensure everything functions correctly.


PreviousUsing git stashNext Working with Remote Repositories

Recommended Gear

Using git stashWorking with Remote Repositories