🚀 Git Rebase & Conflict Resolution: A Step-by-Step Guide
Working in a collaborative Git environment often means rebasing your feature branch with the latest changes from base
. This guide walks you through handling merge conflicts during a rebase, and how to safely push your updated branch back to GitHub.
🧭 Scenario
You’re working on a feature branch (e.g., feature/xyz
) and your pull request (PR) shows:
❌ “This branch cannot be rebased due to conflicts”
To proceed, you need to rebase your branch onto the latest main
, resolve conflicts, and update your branch.
Step-by-Step Instructions
1. Checkout Your Feature Branch
git checkout feature/xyz
2. Fetch the Latest Changes from Remote
git fetch origin
3. Start the Rebase onto base
git rebase origin/base
🔄 If there are conflicts, Git will pause and ask you to resolve them.
4. Resolve Merge Conflicts
Open each conflicted file shown in the terminal (e.g., src/page/index.astro
) and manually fix the conflicts:
- Keep the correct lines
- Delete Git conflict markers like:
<<<<<<< HEAD
=======
>>>>>>> commit-hash
Then stage the resolved file(s):
git add <file-name>
5. Continue the Rebase
git rebase --continue
Repeat Steps 4–5 for all remaining conflicts until rebase completes.
6. If You Get a Vim Commit Message Prompt
If Git opens the commit message editor (like Vim), save and quit:
- In Vim: Press
Esc
, type:wq
, and hitEnter
7. Verify Rebase Success
Once rebase finishes, Git will show:
Successfully rebased and updated refs/heads/feature/RZA-250122.
8. Force Push Your Updated Branch
git push origin feature/xyz --force
⚠️ Force push is necessary because rebasing rewrites commit history.
📝 Optional Pull Request Comment
To inform reviewers, you can add a comment to your PR like:
Rebased onto latest `develop`, resolved conflicts in `index.astro`, and force-pushed. Ready for review ✅
🧯 Bonus Commands
To abort the rebase at any point:
git rebase --abort
To skip a conflicting commit instead of resolving:
git rebase --skip
To check current rebase status:
git status
🏁 You’re Done as your branch is now Synced with the latest base Free of conflicts cleanly rebased ready for review and merge
Thanks For Reading!