Handling Hotfixes and Reverting Merged PRs in Divergent Git Branches
A comprehensive guide to managing hotfixes and reverting PRs while maintaining clean workflows.
In the life of any serious developer, there comes a time when you need to make a fast hotfix to a remote branch while simultaneously working on major local changes that aren't yet ready to be pushed. It's a high-stakes scenario that, if not handled properly, can result in merge conflicts, broken builds, or worse — accidental regressions.
🚨 The Problem
You're working on the release/dev
branch locally, and have a large set of unfinished changes. Suddenly, you need to urgently fix a bug in production. However, your local branch has diverged from the remote version and pushing your current changes isn't an option.
🎯 The Goal
Apply and merge a small hotfix based on the current state of the remote/release/dev
branch—without affecting your local work-in-progress.
🛠️ The Strategy
- 1
Stash or isolate your local work: If uncommitted:
git stash push -m "WIP: my current changes"
If committed but not pushed, you're safe to leave it.
- 2
Fetch latest changes from remote:
git fetch origin
- 3
Create a new hotfix branch based on remote:
git checkout -b hotfix/get-balances origin/release/dev
- 4
Apply and commit your fix:
git add .
git commit -m "Hotfix: fixed GetBalances edge case" - 5
Push and open a PR back to release/dev:
git push -u origin hotfix/get-balances
- 6
Once merged, return to your local branch:
git checkout release/dev
git stash pop # if you stashed changes earlier
🧨 What If the Hotfix Introduced Issues?
You can cleanly revert merged pull requests by creating a clean branch and reverting the merge commits.
- 1
Create a clean branch off remote release/dev:
git fetch origin
git checkout -b temp/revert-release-dev origin/release/dev - 2
Revert the merge commits using their SHAs:
git revert -m 1 <merge-commit-sha>
Repeat for each PR you need to revert. Use
-m 1
to specify that the mainline is the original branch (i.e.,release/dev
). - 3
Push and open a new PR:
git push -u origin temp/revert-release-dev
🧽 Cleaning Up
Once the revert PR is merged and stable:
git checkout release/dev
git fetch origin
git reset --hard origin/release/dev
💡 Pro Tips
- Never force-push to shared branches unless coordinated
- Use
temp/
orwip/
prefixes to indicate disposable or working branches - Document which commits correspond to which PRs for easier traceability
- If merge conflicts appear, always abort (
git merge --abort
) before switching branches
📚 Conclusion
Git can be complex under pressure, but with a well-planned strategy you can safely ship hotfixes, undo mistakes, and avoid unnecessary conflicts. The process outlined here ensures clean collaboration, protects your local progress, and maintains branch hygiene—all critical components of a professional development workflow.
Filed under: Git, Branching Strategy, Hotfix Workflow, Revert Commits, PR Management