Checkout another branch when there are uncommitted changes on the current branch
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you try to switch branches with uncommitted changes, Git either carries the changes to the new branch (if there are no conflicts) or refuses to switch to protect your work. The three main strategies for handling this are: git stash (temporarily shelve changes), committing to the current branch (even as a WIP commit), or using git checkout --merge to attempt a merge. Understanding when Git allows the switch and when it blocks is key to a smooth workflow.
When Git Allows the Switch
Git checks whether your uncommitted changes would be overwritten by the branch switch. If the files you modified are identical on both branches, the switch succeeds and your changes travel with you. If the files differ, Git refuses to avoid losing your work.
Strategy 1: Git Stash
git stash saves both staged and unstaged changes to a stack and reverts the working directory to the last commit. git stash pop restores the changes and removes the stash entry. Use git stash apply to restore without removing.
Strategy 2: WIP Commit
A WIP commit is a normal commit you plan to undo later. git reset --soft HEAD~1 moves the branch pointer back one commit but keeps all changes staged. This approach is cleaner than stash when you want the changes visible in the log temporarily.
Strategy 3: Git Worktree
git worktree lets you check out multiple branches simultaneously in different directories. No stashing or committing needed — your feature branch stays untouched while you work on main in a separate folder.
Strategy 4: Checkout with Merge
--merge attempts to apply your uncommitted changes on the target branch using a merge strategy. This is useful when you started working on the wrong branch and want to move your changes to the correct one.
Moving Uncommitted Changes to a New Branch
Creating a new branch from the current HEAD always succeeds because the new branch starts at the same commit — there are no conflicting file differences.
Common Pitfalls
- Assuming stash is a safe long-term storage:
git stashentries can be accidentally dropped withgit stash droporgit stash clear. Stashes are not pushed to remotes. For work you want to preserve, commit it to a branch instead of relying on the stash stack. - Using
git checkout -fto force switch:git checkout -f maindiscards all uncommitted changes permanently. This is irreversible — there is no way to recover unstaged changes after a force checkout. Only use-fwhen you intentionally want to discard your work. - Forgetting to
stash popafter switching back: Stashed changes sit in the stash stack until explicitly applied. Developers often stash, switch branches, finish their work, and forget to restore the stash. Usegit stash listregularly to check for forgotten stashes. - Stashing only staged changes unintentionally:
git stashsaves both staged and unstaged changes by default. However,git stash --keep-indexonly stashes unstaged changes, which can be confusing. Know which variant you are using to avoid partial stashes. - Not understanding when changes carry over between branches: If you modify a file that is identical on both branches,
git checkoutsilently carries your changes to the new branch. This can be surprising — you may accidentally commit changes to the wrong branch if you do not checkgit statusafter switching.
Summary
- Git allows branch switching with uncommitted changes if there are no file conflicts
- Use
git stashto temporarily shelve changes, thengit stash popto restore - Use a WIP commit +
git reset --soft HEAD~1for a more explicit approach - Use
git worktreeto work on multiple branches simultaneously without stashing git checkout -b new-branchalways succeeds and carries uncommitted changes- Never use
git checkout -funless you intentionally want to discard all uncommitted work

