Git
Version Control
Branching
Merge
Uncommitted Changes

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.

Browse interview questions

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:

bash
git switch target-branch

Older syntax:

bash
git checkout target-branch

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:

bash
git stash push -m "move work to target branch"
git switch target-branch
git stash pop

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:

bash
1git add .
2git commit -m "WIP: partial implementation"
3git switch target-branch
4git cherry-pick <commit-hash>

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:

bash
git stash push -u -m "move work with untracked files"

Then:

bash
git switch target-branch
git stash pop

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:

bash
git stash apply

instead of:

bash
git stash pop

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:

  1. try git switch target-branch,
  2. if Git refuses, use git stash push -u,
  3. switch branches,
  4. apply the stash,
  5. resolve any conflicts,
  6. 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-branch can carry your uncommitted changes directly.
  • If switching would overwrite files, stash first, then switch, then reapply the stash.
  • Use git stash push -u when new untracked files are part of the work.
  • For important work, a temporary commit plus cherry-pick is often safer than a stash.
  • This is really about moving working-tree changes, not about merging committed branch history.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.