master
(and Push Safely)Rebasing is a great way to keep your feature branch up-to-date with master
in a clean, linear history. This guide walks through the correct steps, including how to resolve errors when pushing.
git checkout feature-branch
Make sure you have the latest version of master
from the remote.
git fetch origin
master
git rebase origin/master
This command rewrites your local feature branch history to appear as if it started from the latest origin/master
.
If Git pauses with a conflict, fix the files manually, then run:
git add <file-you-fixed>
git rebase --continue
Repeat this process until the rebase is complete.
To cancel the rebase entirely:
git rebase --abort
Rebase rewrites history, so you must force push:
git push --force
Or safer:
git push --force-with-lease
--force-with-lease
ensures you don't overwrite others' changes unexpectedly.
If you try to push without force after rebasing, you'll get:
! [rejected] branch-name -> branch-name (non-fast-forward)
error: failed to push some refs to 'ssh://bitbucket/repo.git'
This means your branch history has diverged, and a normal push won't work.
Solution: Use --force
or --force-with-lease
as shown above.
rebase
over merge
if you want a cleaner commit timeline.Step | Command |
---|---|
Checkout Branch | git checkout feature-branch |
Fetch Master | git fetch origin |
Rebase | git rebase origin/master |
Resolve Conflicts | git add <file> , then git rebase --continue |
Force Push | git push --force or git push --force-with-lease |