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¶
- Create a branch called
feature-1fromdev. Make two commits.
- Go to
devbranch and make two new commits there.
- Go to
feature-1and rebasedevonto it (bringdevcommits belowfeature-1):git rebase dev
-
If pushing
feature-1for the first time, use:git push origin feature-1 -
If you have already pushed
feature-1before, you must force push because history was rewritten:git push origin feature-1 -f
Warning: Use
git rebasecarefully. 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