Git rebase integrates changes by replaying your commits on top of the latest state of another branch, creating a cleaner, linear history.
- Moves commits to the tip of the target branch.
- Rewrites commit history instead of creating a merge commit.
- Keeps project history linear and easier to read.

Syntax:
git checkout <feature-branch>
git rebase <base-branch>- <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
Imagine you’re working on a feature while the main project gets new updates, and you want to bring those latest changes into your work before finishing.
Think of it like this:
- You are writing a paragraph for a report.
- While you are writing, someone else updates the report with new lines.
- Git rebase moves your changes on top of the latest version, making it look like they were added last.
Example:
1. You and your teammate start with this:
A → B (main branch)2. You create your feature branch and make some changes:
A → B (main)
\
C → D (feature)3. Meanwhile, your teammate updates the main branch:
A → B → E → F (main)
\
C → D (your branch is outdated now)4. Now you run:
git checkout feature
git rebase main5. Your feature branch is moved and now looks like:
A → B → E → F → C' → D' (rebased feature branch)Your work (C and D) is now placed after the latest work (E and F). The code history is clean and easy to follow.
Understanding the Rebase Process
- Git temporarily removes your changes.
- Then it adds the latest work from the branch you rebased onto.
- Then it reapplies your changes on top of that.
- It creates new commit versions of your changes (called C′ and D′).
Visual Working of Git Rebase

Before Rebasing
C1 → C2 → C3 → C4 → C5 (Main Branch)
\
B1 → B2 → B3 (Feature Branch)- C1 to C5 are commits on the main branch (green).
- B1 to B3 are commits on the feature branch (yellow), created from C3 .
- At this point, the feature branch is outdated, because the main branch has moved ahead ( C4 and C5 were added after C3).
After Rebasing B1, B2, and B3
C1 → C2 → C3 → C4 → C5 → C6 → C7 → C8
↑
(Rebased Feature Commits)- The git rebase command takes commits B1 → B2 → B3 and reapplies them on top of C5, the latest commit on main.
- These become new commits: C6 → C7 → C8 (green versions of B1–B3).
- The feature branch now appears as if it was based on the latest version of the main branch.
Internal Workflow of Git Rebase
- Git finds the common ancestor commit (C3) between the main and feature branches.
- Temporarily removed commits B1, B2, and B3.
- Moved the base of the feature branch from C3 to C5.
- Re-applied the changes from B1-B3 on top of C5 as new commits C6, C7, C8.
Types of Git Rebase
Interactive Rebase (git rebase -i)
- Allows editing, squashing, reordering, or deleting commits.
- Combines multiple commits into a single clean commit.
- Helps reorder commits for a clearer history.
- Enables editing commit messages before pushing.
Non-Interactive Rebase (Standard Rebase)
- Automatically reapplies commits onto the target branch using git rebase <branch>.
- Does not allow manual editing of commits during the rebase.
- Used to update a feature branch with the latest changes from the main branch.
Auto-merge Rebase
- Automatically applies commits when no conflicts are found.
- Stops and requires manual resolution if conflicts occur.
- Commonly used to keep feature branches up to date with the main branch.
Git Standard vs Git Interactive Rebase
Git rebase has two modes standard (default) and interactive using the -i option.
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 checkout branch_x
git rebase master- If you are already on branch_x, this is equivalent to:
git rebase master- Here, Git automatically takes the commits from the current branch and reapplies them on top of 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 masterThis command lists upcoming commits and lets you edit or reorder them to keep a clean project history.
Common Git Rebase Commands
The following are the most used Git rebase commands:
- git rebase master: Applies the changes of the current branch onto the master branch.
- git rebase --continue: Continues the rebase process after resolving conflicts
- git rebase --abort: Cancels the ongoing rebase and restores the branch to its original state
- git rebase --skip: Skips a commit if conflicts arise, though this is not recommended as it could damage your codebase.
- 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 Abort
- If you wish to undo a rebase, use the git reset command:
git reset --hard <branch-name>- This resets your branch to its previous state before rebasing.
Steps to Recover Lost Changes Process of Upstream Rebase
This process helps restore commits or changes that may be lost after rebasing against an upstream branch.
Step 1: Check the Reflog
Use the following command to view the commit history, including the lost commits:
git reflogStep 2: Create a New Branch
Once you find the commit ID, create a new branch from it:
git branch <new-branch> <commit-id>Step 3: Cherry-pick Lost Commits
The following command is used to apply specific (lost or required) commits to another branch:
git cherry-pick <commit id> Step 4: Resolve Conflicts
Use git add <file> to stage resolved files.
git add <file>Step 5: Push the New Branch
To push your new branch to the remote repository use the following command:
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 --rebaseBest Practices for Using Git Rebase
Use git rebase to keep commit history clean and up to date while avoiding changes to shared branches and resolving conflicts promptly.
- Don’t Rebase Public History: Avoid rebasing shared branches to prevent disrupting others’ work.
- Clean Up Commits with Rebase: Squash or organize commits before merging to main.
- Rebase Regularly: Keep feature branches updated to reduce future conflicts.
- Resolve Conflicts Quickly: Fix conflicts promptly during rebasing.

