What you are about to do and why it matters
If you want a commit history that reads like a polite straight line instead of an emotional labyrinth this guide shows how to rebase master onto a feature branch. You will update your feature branch with the latest master changes while keeping history linear. This is a git workflow trick that makes code review and bisecting less painful for everyone involved.
Prerequisites
- Familiarity with basic git commands and a working feature branch
- Remote named origin or adjust commands to match your remote
- Comfort with resolving merge conflicts and using
git rebase --abort
if things go sideways
Quick overview of the steps
- Switch to your feature branch
- Fetch the latest master from remote
- Rebase your feature branch onto origin master
- Resolve conflicts if any and continue the rebase
- Push the rewritten branch using force with lease
Step 1 Check out the feature branch
Make sure HEAD points at the branch you want to update. No magic here.
git checkout feature-branch
Step 2 Fetch the latest master
Bring the remote changes into your local view without touching your branches yet. This grabs origin/master
so the rebase uses the latest code.
git fetch origin
Step 3 Rebase the feature branch onto master
While staying on your feature branch run the rebase. This rewrites your feature commits on top of the latest master commits for a clean linear history.
git rebase origin/master
What happens during the rebase
Git will replay your feature branch commits one by one onto origin master. If everything is simple this is fast and drama free. If conflicts appear git will pause and let you play firefighter.
Step 4 Handle conflicts like a civilized developer
If a conflict pops up do this
- Edit the conflicted files to resolve the issues
- Stage the fixes with
git add
- Resume the rebase with
git rebase --continue
If you decide the rebase is a terrible idea use git rebase --abort
to return to the state before the rebase started. No harm done other than bruised ego.
Step 5 Push the rewritten branch safely
Because rebase rewrites commits you need to update the remote branch with a forced push. Use force with lease to avoid stomping on someone else work by accident.
git push --force-with-lease
Tips and best practices
- Prefer running
git rebase origin/master
from the feature branch instead of merging master into it. It keeps history tidy. - Use
--force-with-lease
instead of a plain force push. It is the polite version of nuclear force. - Communicate with your team when you rewrite shared branches. Telepathy is not a listed collaboration strategy.
- Keep small focused commits during feature work. Smaller conflicts are less dramatic.
Troubleshooting
If the rebase stops unexpectedly check the state with git status
and inspect the patch with git log --oneline --graph
. If a resolution feels risky abort and regroup. If something strange happens you can always recover provided you have backups or reflog patience.
Wrap up
Rebasing master onto a feature branch is a simple way to keep your work up to date and your commit history readable. Follow the steps here and keep git rebase --abort
handy in case the universe decides to be dramatic. Happy rebasing and may your merge conflicts be mercifully short.