How to Rebase a Feature Branch from Master (Step-by-Step Guide)

How to Rebase a Feature Branch from 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.


✅ 1. Checkout Your Feature Branch

git checkout feature-branch

✅ 2. Fetch Latest Changes from Remote

Make sure you have the latest version of master from the remote.

git fetch origin

✅ 3. Rebase Your Branch Onto master

git rebase origin/master

This command rewrites your local feature branch history to appear as if it started from the latest origin/master.


✅ 4. Resolve Any Merge Conflicts (If Any)

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

✅ 5. Force Push to Remote (After Rebase)

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.


🛑 Common Error: Non-Fast-Forward Rejection

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.


⚠️ Important Notes

  • Avoid rebasing shared branches unless all contributors are aligned.
  • Force push with care: it changes commit history.
  • Prefer rebase over merge if you want a cleaner commit timeline.

✅ Summary

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

Tags: tech cheat-sheet git version control development command line tutorial devops