
How to Move a Git Branch to a Different Parent
You branched off feature-A , did your work, then realized your branch should have been based on main instead. Now your branch carries all of feature-A 's commits along with yours. Here's how to fix it in 4 steps. The Problem main ---- A ---- B \ feature-A ---- C ---- D \ your-branch ---- E <-- your commit You only want commit E , but your branch includes C and D from feature-A . The Fix 1. Find your commit hash git log --oneline your-branch --not main This lists all commits on your branch that aren't in main . Identify yours — in this case, E . 2. Create a clean branch from main git checkout main git pull origin main git checkout -b your-branch-clean 3. Cherry-pick your commit git cherry-pick <commit-hash> This applies only your commit on top of main . 4. Replace the old branch git branch -D your-branch git branch -m your-branch-clean your-branch git push origin your-branch --force-with-lease Result main ---- A ---- B \ your-branch ---- E Clean branch, no extra baggage. Why cherry-pick
Continue reading on Dev.to Webdev
Opens in a new tab

