Move existing, uncommitted work to a new branch in Git
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you started editing on the wrong branch, you can move uncommitted work to a new branch without losing changes. Git keeps working tree and index changes when you create and switch branches in place. The key is creating the branch first, then committing on the correct branch.
Fastest Safe Workflow
From the branch with current uncommitted changes:
Your modified files remain present after the switch. Now commit normally on the new branch.
This is the standard approach and usually all you need.
If You Already Committed on Wrong Branch
If changes were committed accidentally on main branch, move them by creating a new branch at current commit and resetting the original branch.
Then continue work on feature/new-topic.
Only use hard reset if commit has not been pushed and you are sure about history rewrite implications.
Use Stash When Branch Switch Is Blocked
Sometimes branch switch fails due to conflicts with tracked file changes. In that case, stash, switch, then pop.
Resolve any conflicts and commit on the new branch.
Verify Target Branch Before Committing
Add a small habit to avoid repeat mistakes:
Running these checks before every commit prevents work from landing on the wrong branch.
Collaboration Considerations
If your wrong branch is shared and protected, do not rewrite published history without team coordination. Instead, create a corrective commit or revert workflow according to team policy.
For private local branches, switching and committing on a new branch is usually risk free.
Keep Staged and Unstaged Changes Intact
If you care about preserving staged state exactly, verify index status before and after branch switch. Git keeps both staged and unstaged changes when switching to a newly created branch from the same commit.
If branch switching is blocked by conflicts, stash with index preservation:
This workflow helps when you intentionally staged only part of a change and want to keep commit boundaries clean. It is also useful when hotfix and feature edits overlap in the same working tree during release pressure and fast patch cycles in production environments.
Common Pitfalls
A common mistake is committing first and then forgetting to move history, which requires extra cleanup commands.
Another issue is using git reset --hard without confirming whether commits were already pushed. This can disrupt collaborators.
A third issue is stashing with untracked files omitted by default when needed files are new. Use -u to include untracked paths.
Summary
- Use
git switch -cto create a new branch and keep uncommitted changes. - Commit work on the new branch after switching.
- If wrong commits exist, branch then reset only when safe.
- Use stash flow when direct switching is blocked.
- Verify current branch before commit to avoid repeat errors.

