Move existing, uncommitted work to a new branch in Git
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Starting work on the wrong Git branch is common, and in most cases the fix is simple. Uncommitted changes live in your working tree and index, not in branch history yet, so they usually move with you when you create and switch to a new branch. The safest recovery depends on whether the changes are unstaged, staged, blocked by checkout conflicts, or already committed.
The Usual Fix Is git switch -c
If Git allows the branch switch, the simplest solution is to create a new branch from the current commit and carry the working tree changes with you.
After that, your modified files are still there, but now you are on the correct branch. You can stage and commit normally.
This should be the first attempt because it is the lowest-risk path.
Use stash Only When the Branch Switch Is Blocked
Sometimes Git refuses to switch because the target branch would overwrite your current changes. In that case, stash the work, create the branch, and restore it there.
The -u flag matters because it includes untracked files. Without it, new files can be left behind and the move feels incomplete.
If conflicts appear during stash pop, resolve them like merge conflicts. The key point is that the stash is only a fallback when a direct branch creation is blocked.
Staged Work Moves Too
If you already staged some changes, git switch -c preserves that state. This is useful when you intentionally prepared part of the commit and want to keep that work intact.
If you later realize the branch should contain multiple clean commits rather than one large one, use interactive staging.
That keeps the history reviewable without redoing your work.
If You Already Committed on the Wrong Branch
Once the work is committed, the problem changes. The usual move is to create the new branch at the current commit first.
Now the commit is on the right branch too. If the commit should no longer remain on the original branch and it has not been shared, you can move the old branch pointer back.
That command is destructive and safe only if the commit has not been pushed or shared. If it has been shared, use a safer history-preserving approach such as revert or coordinated rewrite.
A Temporary WIP Commit Can Be Safer Than a Stash
Some developers prefer a temporary commit because it is easier to recover through reflog than a stash they forgot about.
You can later squash or amend that commit into cleaner final history. This is especially useful when the working tree is large and you want a clearly recoverable checkpoint before moving things around.
Verify Before the First Push
Before pushing the new branch, quickly verify the branch name, recent commits, and diff against the intended base.
That short check catches wrong-branch mistakes before they become pull request cleanup work.
Common Pitfalls
- Using
git reset --hardbefore confirming the work exists safely on another branch or in a stash. - Forgetting
-uwhen stashing and leaving untracked files behind. - Treating stash as the first tool even when
git switch -cwould have worked directly. - Combining unrelated edits into one recovery commit and making later cleanup harder.
- Skipping verification and discovering the branch mistake only after opening a pull request.
Summary
- For ordinary uncommitted work,
git switch -c new-branchis usually enough. - If switching is blocked, stash including untracked files and restore on the new branch.
- Staged changes move with the branch switch too.
- If the work is already committed, move the commit to the right branch before cleaning the old one.
- Verify branch, commits, and diff before the first push.
Related reading
- Move the most recent commits to a new branch with Git
- Moving a git repository up one hierarchy level
- moving changed files to another branch for check-in
- moving committed but not pushed changes to a new branch after pull
- Moving from CVS to Git Id equivalent?
- Moving Git repository content to another repository preserving history
- Moving uncommitted changes to a new branch
- MS-SQL Server 2005 Initializing a merge subscription with alternate snapshot location
.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.