Git Rebase - If you build features on branches while your teammates keep updating main, your branch falls behind. Syncing with merge can clutter history; rebase keeps it clean. Here’s a practical, production-ready guide you can follow before opening a PR, with crisp Practical guide & examples and copy-pastable commands.
TOC (Git Rebase - Practical guide and examples)
What Rebase Does and Why It Helps
Rebase vs Merge
Team Policies to Consider
How to Rebase Before a PR
Conflict Handling and Safety
Pull Strategies
Interactive Rebase
What “rebase” actually means
You have commits on a feature branch based on an older point of main.
Rebase lifts your commits and replays them on top of the latest main.
You end up with the same changes, but based on the newest base.
Visual model Before:
1main: A --- B --- C --- D2feature: X --- Y3
After git rebase origin/main
:
1main: A --- B --- C --- D2feature: X' --- Y'3
X' and Y' are your changes replayed on top of D.
Rebase vs merge (quick comparison)
Merge
Pros: Preserves actual integration history; simple.
Cons: Adds merge commits; history can get noisy.
Rebase
Pros: Linear, readable history; easier for reviews, git log, and git bisect.
Cons: Rewrites commits (new SHAs); coordinate before rebasing shared branches.
Safe rule of thumb: Rebase your own feature branches to sync with main. Your team can still integrate via PR using merge, squash, or “rebase and merge” depending on policy.
Team policies and when to prefer each
Linear-history repos:
Require rebase or squash. You’ll likely need to rebase locally and push with lease.
Merge-commit repos:
Prefer merging main into your branch; rebasing shared branches may be discouraged.
PR strategies:
Squash merge keeps a clean main without requiring local rebase.
Rebase and merge rewrites your branch on the server for linear history.
Protected branches:
If force-push is disabled, avoid rebasing after publishing; either coordinate, or open a new branch/PR.
Fast-forward preference:
Some teams set pulls to fast-forward only:
1git config --global pull.ff only
Step-by-step: Rebase before opening a PR Scenario:
Branch: feature/login-form
main has moved ahead
Save your work Option A — commit WIP:
1git add .2git commit -m "WIP: current changes"3
Option B — stash instead of WIP commit:
1git stash -u2
Update local view of remote
1git fetch origin2
Ensure your local main is current (optional but common)
1git checkout main2git pull origin main3
Switch back and rebase onto the remote main
1git checkout feature/login-form2git rebase origin/main3
What Git does:
Temporarily lifts your commits
Moves your branch to the latest remote main
Replays your commits one by one
Resolve conflicts (if any)
1git status # see conflicted files2# fix files, then:3git add <file> # repeat per file4git rebase --continue5# bail out if needed:6git rebase --abort7
Tips for resolving conflicts
Start with small/obvious files to build momentum.
Use a merge tool:
1git mergetool2
Enable rerere to remember resolutions:
1git config --global rerere.enabled true2
Run tests after resolving:
1npm test # or your test runner2
If you stashed earlier, restore after rebase:
1git stash pop2
Push safely after rebase
1git push --force-with-lease origin feature/login-form2
Why lease: avoids overwriting new remote commits you don’t have. If branch protection forbids force-push, coordinate with the team or push a new branch and open a new PR.
git pull vs git pull --rebase
Default pull (fetch + merge):
1git pull2
History may include a merge commit if there’s divergence:
1A --- B --- C --- M2 \ /3 D --- E ----4
Note: If your branch has no local commits, git pull
fast-forwards without a merge commit.
Pull with rebase (fetch + rebase):
1git pull --rebase2
History stays linear:
1A --- B --- C --- D' --- E'2
Which should you use?
Use
git pull
if your team is fine with merge commits and wants to preserve the integration graph.Prefer
git pull --rebase
if your team wants a clean, linear history—great before opening PRs.
Make pulls smoother
Prefer rebase on pull by default:
1git config --global pull.rebase true2
Auto-stash before a pull rebase:
1git config --global rebase.autoStash true2
Interactive rebase to polish commits Use this to squash fix-ups, reorder, or reword messages before a PR.
1git fetch origin2git checkout feature/login-form3git rebase -i origin/main4
In the editor:
1pick abcd123 feat(login): add form skeleton2pick bcde234 fix: correct validation3pick cdef345 chore: adjust tests4
Tidy by squashing:
1pick abcd123 feat(login): add form skeleton2fixup bcde234 fix: correct validation3fixup cdef345 chore: adjust tests4
Finalize, then force-push safely:
1git push --force-with-lease2
Troubleshooting and recovery
Abort mid-rebase:
1git rebase --abort2
Inspect recent tips and recover with reflog:
1git reflog2git reset --hard <commit> # restore to a known good point3# or create a rescue branch:4git checkout -b rescue/<topic> <commit>5
Note: Rebasing rewrites commit SHAs. Links to old SHAs (e.g., in code reviews) won’t match after a rebase.
Small, focused commits help Keep commits scoped and descriptive. Examples:
1feat(login): validate email format2fix(login): trim whitespace before submit3test(login): add invalid email cases4
Daily workflow you can copy
1# Update remote state and main 2git fetch origin 3git checkout main 4git pull origin main 5 6# Rebase your branch onto remote main 7git checkout feature/xyz 8git rebase origin/main 9 10# Resolve conflicts11git status12# edit files13git add <file>14git rebase --continue15 16# Verify locally17npm run lint18npm test19 20# Push safely21git push --force-with-lease22
Bottom line Git Rebase - Practical guide and examples keeps your feature branches fresh and history clean. Rebase your own branches before PRs, resolve conflicts carefully, and push with --force-with-lease
. Match your approach to team policy: some repos enforce linear history and rebase/squash, others prefer merge commits and disallow force pushes. When in doubt, base onto origin/main
, communicate early, and keep commits small..
References
Git Book: Rebasing
Git Docs: git rebase
Pro Git (free book): Branching, Merging, and Rebasing chapters
GitLab Docs: Merge methods and fast-forward

Comments