Skip to content

Git Rebase Explained

โ† Back to Git


โšก Git Rebase

git rebase is used to synchronize your current branch with a target branch by moving your commits on top of the target branch's latest commit.

๐Ÿ”„ How it Works

In the current branch, the commits coming from the target branch will be placed below the commits done in the current branch (rewriting history).

Pros: * Reduces merge commits. * Creates a linear, cleaner commit history.

๐Ÿ“ Example

  1. Create a branch called feature-1 from dev. Make two commits.

Feature Commits

Feature Log

  1. Go to dev branch and make two new commits there.

Dev Commits

Dev Log

  1. Go to feature-1 and rebase dev onto it (bring dev commits below feature-1):
    git rebase dev
    

Rebase Result

  1. If pushing feature-1 for the first time, use:

    git push origin feature-1
    
    Push

  2. If you have already pushed feature-1 before, you must force push because history was rewritten:

    git push origin feature-1 -f
    

Warning: Use git rebase carefully. It rewrites history, which can cause issues for shared branches.


๐Ÿง  Quick Quiz โ€” Rebase

#

Why might you need to force push (git push -f) after a rebase?


๐Ÿ“ Want More Practice?

๐Ÿ‘‰ Start Git Advanced Quiz (20 Questions)


๐Ÿ“ฌ DevopsPilot Weekly โ€” Learn DevOps, Cloud & Gen AI the simple way.
๐Ÿ‘‰ Subscribe here