Moving uncommitted changes to a new branch
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A common Git workflow mistake is making uncommitted changes on one branch, then realizing those edits belong on a different feature branch. The good news is that Git can move this work cleanly without losing edits if you use the right sequence. The best approach depends on whether branch switching is immediately possible or blocked by conflicts.
Quickest Path: Create New Branch from Current State
If your current working changes do not conflict with branch checkout rules, the simplest command is:
Older equivalent:
This creates a new branch at current commit and keeps your uncommitted changes in working tree and index.
Validate immediately:
This confirms your work is now attached to the new branch context.
When Switching Is Blocked
Sometimes Git blocks checkout because changes would be overwritten by files in target branch. In that case, stash first.
If conflicts appear during stash pop, resolve and continue with normal staging and commit.
Preserve Untracked Files Too
By default, stash may skip untracked files. If you created new files that need to move with your work:
Then branch-switch and pop as usual.
Move Only Part of Your Changes
Sometimes only subset of edits belongs to new branch. Split changes using interactive staging.
Remaining unstaged changes can be stashed or committed separately. This keeps branch history focused and review-friendly.
Scenario: You Already Committed on Wrong Branch
If mistake is discovered after commit:
- create new branch at current commit
- move original branch pointer back if needed
Use hard reset only when you are certain no shared history depends on that commit.
Safe Recovery with Reflog
If you make a wrong move, reflog can usually recover branch heads.
Keeping this recovery option in mind makes branch correction less risky.
Push and Track the New Branch
After you commit moved changes, publish with upstream tracking.
This enables simpler future git push and git pull commands.
Team Workflow Best Practices
To avoid repeated branch-move corrections:
- create branch before first code change
- use issue-based branch names
- keep commits small and focused
- run
git statusandgit branch --show-currentbefore committing
These habits reduce cleanup work and improve pull request clarity.
Practical Decision Guide
Use this quick decision tree:
- no checkout conflict and all work should move:
git switch -c - checkout conflict present: stash then branch
- only partial work should move: branch then
git add -p - already committed on wrong branch: create new branch and reset old branch carefully
Selecting based on situation prevents data loss and messy history.
Common Pitfalls
- Continuing to commit on wrong branch after noticing the mistake.
- Using hard reset without confirming commit safety.
- Forgetting untracked files when stashing work for branch migration.
- Moving all changes when only a subset belongs to new task.
- Skipping verification of active branch before commit.
Summary
- Uncommitted changes can usually be moved safely by creating a new branch directly.
- Stash is the fallback when checkout is blocked by file conflicts.
- Interactive staging helps split mixed work into correct branches.
- Reflog provides recovery safety if branch operations go wrong.
- Early branch discipline is the best prevention for misfiled local changes.
Related reading
- MS-SQL Server 2005 Initializing a merge subscription with alternate snapshot location
- multi-way merge vs 2-way merge
- Multiple GitHub accounts on the same computer?
- Multiple GitHub accounts on the same computer?
- Multiple .gitignore in subfolders
- My Git bash forgets my aliases. What can I do?
- Need to reset git branch to origin version
- New file created in Xcode 9.3, wsname.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist should it be committed?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.