Open In App

Git Rebase

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Git Rebase is a command used to move or combine a sequence of commits to a new base commit. It helps you put your changes on top of a different commit, making the history cleaner and more straightforward without creating merge commits. Unlike git merge, which combines branches and adds a merge commit, rebase rewrites history by applying your changes directly to the target branch (usually master or main). This keeps the history neat and easier to understand.

Syntax:

git checkout <feature-branch>
git rebase <base-branch>

In this syntax:

  • <feature-branch> is the branch with the changes you want to rebase.
  • <base-branch> is the branch you want to rebase your changes onto, typically main or master.

When to Use Git Rebase

You can use git rebase in the following situations:

  • Clean up commit history: If you’ve made multiple small commits or fixes that you want to combine into one commit for a cleaner history.
  • Stay up-to-date with the base branch: If you’re working on a feature branch and want to incorporate changes from the master branch into your branch without creating merge commits.
  • Prepare a branch for merging: Before merging a feature branch into the master branch, you can use rebase to make sure your branch’s changes are applied on top of the latest master branch.

What Happens During Rebase?

  • Git temporarily removes your commits from the feature-branch.
  • Git updates the branch to the latest commit from master (or another branch you specify).
  • Git re-applies your commits on top of the latest commit from master, one by one.
  • If there are any conflicts, Git will stop and ask you to fix them.

Once the rebase is complete, your feature-branch will appear as if all your changes were made directly on top of the latest master branch, resulting in a cleaner history.

How Git Rebase Works?

When you execute a git rebase, Git performs the following steps:

  • Identify the common ancestor: Git first finds the last common commit between the branch you’re working on and the branch you’re rebasing onto (usually the main or master branch).
  • Rewind your commits: It temporarily “removes” your commits from the current branch, effectively “rewinding” the changes you’ve made.
  • Apply the changes to the new base: Git then takes each of your commits and applies them one by one to the latest commit of the branch you’re rebasing onto. If conflicts occur during this process, Git will pause and prompt you to resolve them.
  • Update the current branch pointer: After all commits are applied, Git updates the pointer of your branch to point to the newly rebased branch, which now includes your changes.
  • Clean history: The result is a linear history where the changes from your branch appear to have been made on top of the latest commit of the target branch
Rebasing-in-git

Git Rebase

Types of Git Rebase

1. Interactive Rebase (git rebase -i)

  • This allows you to edit, squash, reorder, or delete commits in your branch. It gives you full control over the commit history, making it useful for cleaning up commit messages or combining multiple commits into one.
  • It squashing commits to combine them into a single commit.
  • Git rebase reordering commits to reflect a more logical flow.
  • Editing commit messages before pushing them to a remote repository.

2. Non-Interactive Rebase (Standard Rebase)

  • This is the regular rebase command (git rebase <branch>), which simply applies your commits onto the target branch without allowing for manual intervention. It’s ideal for straightforward rebasing where you don’t need to modify or review individual commits.
  • Updating your feature branch with the latest changes from the main branch.

3. Auto-merge Rebase

  • When rebasing, Git will automatically merge changes if there are no conflicts between the commits being rebased and the target branch. If conflicts are detected, Git will stop and require manual resolution.
  • It rebasing feature branches frequently to stay up-to-date with the main branch.

Uses of Git Rebase 

The primary goal of rebasing is to maintain a cleaner, linear project history, which simplifies navigation through the commit history. It provides a straight line from the beginning to the end commit of a feature, improving readability.

  • Keeping a clean and linear commit history: Rebasing eliminates unnecessary merge commits, offering a linear history.
  • Updating feature branches: Rebasing keeps a feature branch up to date with the latest changes from the main branch.
  • Resolving merge conflicts: Rebasing allows conflicts to be resolved at each stage, ensuring a clean merge.
Use of git rebase

Uses of Git Rebase

Git Standard vs Git Interactive Rebase

Git rebase operates in two modes: Standard and Interactive. The mode is determined by whether the –i (interactive) flag is used. Without any arguments, Git runs rebase in Standard mode.

Standard Rebase:

Standard rebasing applies all commits from the current branch to the head of the target branch without manual intervention. The following command performs a standard rebase:

git rebase master branch_x
  • This is equivalent to:
git rebase master
  • Here, Git automatically takes the commits from your current branch and applies them to the specified branch (master).

Interactive Rebase:

Interactive rebasing allows you to edit, reorder, squash, or drop commits before applying them to the new branch. This gives you full control over the branch’s commit history.

git checkout branch_x
git rebase -i master
  • This command lists all commits that are about to be moved and prompts you to edit or rearrange them based on your choices. It helps maintain a clean and structured project history.

Common Git Rebase Commands

The following are the most used Git rebase commands:

  1. git rebase master: Applies the changes of the current branch onto the master branch.
  2. git rebase –continue: Continues the rebase process after resolving conflicts
  3. git rebase –abort: Cancels the ongoing rebase and restores the branch to its original state
  4. git rebase –skip: Skips a commit if conflicts arise, though this is not recommended as it could damage your codebase.
  5. git rebase -I HEAD~3: Starts an interactive rebase on the last three commits, allowing edits like commit message changes.

Configuration Options In Git Rebase

Customize your rebase process with options like:

  • –interactive (-i): Enables interactive rebasing for editing commits.
  • –onto <newbase>: Specifies a new base commit for rebase.
  • –no-verify: Skips pre-commit hooks during rebase.
  • –auto-squash: Automatically squashes commits marked with fixup or squash

Git Rebase vs Merge

While both commands integrate changes from one branch into another, the difference lies in the commit history:

  • Git Merge: Maintains a non-linear history with merge commits.
  • Git Rebase: Results in a clean, linear history without merge commits, making it more suitable for advanced developers.
git merge branch_x master
Git Rebase Git Merge
Rewrites commit history, leading to a cleaner, linear history. Keeps commit history as is, leading to a more complex, branching history.
No merge commit is created, making the history easier to follow. A merge commit is created, which can clutter the history.
Can be used to update a feature branch with the latest changes from the base branch. Often used to integrate feature branches into the main branch.
Best for cleaning up commit history before merging. Best for preserving history and when you want to maintain the exact sequence of events.

Git Rebase vs Merge

Note: After performing rebasing, we are by default on the last commit of the rebased branch.

Git Rebase Abort

  • If you wish to undo a rebase, use the git reset command:
git reset --hard <branch-name>
  • “– hard” is like a hard option and we use “branch name”. By doing the above two steps you can undo the changes.

Steps to Recover Lost Changes Process of Upstream Rebase

Step 1: Check the Reflog: Use git reflog to find commit IDs for lost changes.

Step 2: Create a New Branch:

git branch <new-branch> <commit-id>

Step 3: Cherry-pick Lost Commits:

git cherry pick <commit id> 

Step 4: Resolve Conflicts: Use git add <file> to stage resolved files.

Step 5: Push the New Branch:

git push -u origin <new-branch>

Git Pull Rebase

The git pull –rebase command fetches updates from the remote repository and re-applies your local commits on top of those changes, resulting in a linear commit history.

Steps:

  • Fetch the remote repository:
git fetch <remote>
  • Rebase the local branch:
git pull --rebase

Advantages of Git Rebase

Here are some advantages of Git rebase:

  • Cleaner Commit History: Rebasing creates a more readable, linear history.
  • Simplified Code Understanding: A streamlined commit history aids in debugging and understanding code changes.
  • Up-to-Date Repositories: Rebasing ensures your branch stays up to date with the latest changes in the main branch.
  • Improved Collaboration: Rebasing reduces merge conflicts and simplifies integrating multiple branches.

Disadvantages of Git Rebase

Here are some disadvantages of Git rebase:

  • Complexity: Rebasing can be difficult, especially with complicated commit histories.
  • Risk of Lost Commits: If a rebase isn’t performed correctly, commits may be lost.
  • Challenges with Public Repositories: Rebasing changes the commit history, which can disrupt collaboration on shared repositories.
  • Breaks Downstream Branches: Downstream branches may encounter merge conflicts after rebasing.

Best Practices for Using Git Rebase

  • Don’t Rebase Public History: Don’t rebase branches that are already shared with others. Rebase changes the history, which can mess things up for your teammates.
  • Use Rebase to Clean Up Commits: Before merging your feature branch into the master branch, use rebase to tidy up your commits, combining small ones into one clear commit.
  • Rebase Regularly: Keep your feature branch up-to-date by regularly rebasing it on the latest master or main branch. This helps avoid conflicts later.
  • Resolve Conflicts Quickly: If conflicts happen while rebasing, fix them as soon as possible. Don’t let them pile up.

Merge vs Rebase

Both merge and rebase are used to bring changes from one branch to another in Git, but they do it in different ways. Below is the comparison between them:



Next Article

Similar Reads