How do I merge my local uncommitted changes into another Git branch?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Uncommitted changes do not belong to a branch until you commit them, but Git can often carry them with you when you switch branches. The safest answer depends on whether the target branch can accept the current working-tree changes cleanly: if yes, you can switch directly; if not, stash or commit first.
The Simplest Case: Just Switch Branches
If your current uncommitted changes do not conflict with the target branch, Git can move them with you:
Older syntax:
If the checkout succeeds, your uncommitted edits come along into the target branch's working tree. No stash is required.
This is the cleanest solution when it works.
When Git Refuses to Switch
Git blocks the branch change if switching would overwrite files or create conflicts with the target branch's content.
In that case you will see a message telling you local changes would be overwritten. That is Git protecting your work.
When that happens, the standard safe solution is to stash the changes:
Now the uncommitted work is applied on the target branch instead.
Use a Commit When the Work Is Meaningful
If the changes are substantial, a temporary commit is often safer and more auditable than a stash:
This is especially helpful when:
- you want a clear recovery point,
- the changes may sit around for a while,
- or you want the work to survive across machines or rebases.
Stashes are convenient, but commits are often easier to inspect and less likely to be forgotten.
Include Untracked Files If Needed
By default, git stash may leave some untracked files behind. If your work includes new files, use:
Then:
Without -u, you may switch branches and then wonder why some new files did not come along.
Applying Instead of Popping
If you want to keep the stash entry until you are sure the apply worked, use:
instead of:
apply leaves the stash in the stash list. pop applies it and removes it if the apply succeeds.
That small difference can matter when the change set is large or risky.
A Good Practical Workflow
A reliable decision tree is:
- try
git switch target-branch, - if Git refuses, use
git stash push -u, - switch branches,
- apply the stash,
- resolve any conflicts,
- then commit on the correct branch.
This keeps your history clean because the final commit lands where it belongs.
Why This Is Not a "Merge" in the Git Sense
The title says "merge my uncommitted changes," but technically there is no Git merge object involved until you commit and merge actual branch history.
Before commit, you are really moving working-tree changes from one branch context to another. Git can help with that, but it is not the same as merging two commit histories.
That distinction matters because it explains why stash and checkout behavior are the main tools here.
Common Pitfalls
The biggest pitfall is assuming uncommitted work is already attached to the current branch in a durable way. It is not. Until you commit, it is just working-tree state.
Another mistake is using git stash without -u and then forgetting that untracked files may remain behind.
Developers also sometimes force switches or reset commands when a simple stash would have preserved everything safely. There is rarely a good reason to get destructive here.
Finally, if the work is meaningful and you care about traceability, consider a temporary commit instead of a stash. Git history is often safer than memory.
Summary
- If Git allows it,
git switch target-branchcan carry your uncommitted changes directly. - If switching would overwrite files, stash first, then switch, then reapply the stash.
- Use
git stash push -uwhen new untracked files are part of the work. - For important work, a temporary commit plus
cherry-pickis often safer than a stash. - This is really about moving working-tree changes, not about merging committed branch history.
Related reading
- How do I merge two dictionaries in a single expression in Python?
- How do I migrate an SVN repository with history to a new Git repository?
- How do I migrate an SVN repository with history to a new Git repository?
- How do I modify a specific commit?
- How do I move an existing Git submodule within a Git repository?
- How do I name and retrieve a Git stash by name?
- How do I override Git configuration options by command line parameters?
- How do I ''overwrite'', rather than ''merge'', a branch on another branch in Git?
.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.