existing
move
uncommitted
git

Move existing, uncommitted work to a new branch in Git

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you started editing on the wrong branch, you can move uncommitted work to a new branch without losing changes. Git keeps working tree and index changes when you create and switch branches in place. The key is creating the branch first, then committing on the correct branch.

Fastest Safe Workflow

From the branch with current uncommitted changes:

bash
1git status
2
3git switch -c feature/new-topic
4# older Git alternative
5# git checkout -b feature/new-topic
6
7git status

Your modified files remain present after the switch. Now commit normally on the new branch.

bash
git add .
git commit -m "Implement new topic changes"

This is the standard approach and usually all you need.

If You Already Committed on Wrong Branch

If changes were committed accidentally on main branch, move them by creating a new branch at current commit and resetting the original branch.

bash
1# on wrong branch with bad commit at HEAD
2git branch feature/new-topic
3
4git switch main
5git reset --hard HEAD~1

Then continue work on feature/new-topic.

Only use hard reset if commit has not been pushed and you are sure about history rewrite implications.

Use Stash When Branch Switch Is Blocked

Sometimes branch switch fails due to conflicts with tracked file changes. In that case, stash, switch, then pop.

bash
git stash push -u -m "temp move work"
git switch -c feature/new-topic
git stash pop

Resolve any conflicts and commit on the new branch.

Verify Target Branch Before Committing

Add a small habit to avoid repeat mistakes:

bash
git branch --show-current
git status --short

Running these checks before every commit prevents work from landing on the wrong branch.

Collaboration Considerations

If your wrong branch is shared and protected, do not rewrite published history without team coordination. Instead, create a corrective commit or revert workflow according to team policy.

For private local branches, switching and committing on a new branch is usually risk free.

Keep Staged and Unstaged Changes Intact

If you care about preserving staged state exactly, verify index status before and after branch switch. Git keeps both staged and unstaged changes when switching to a newly created branch from the same commit.

bash
1git status --short
2# example output:
3# M  src/app.js
4#  M README.md
5
6git switch -c feature/fix-branch-target
7git status --short

If branch switching is blocked by conflicts, stash with index preservation:

bash
git stash push --keep-index -u -m "move-work-with-index"
git switch -c feature/fix-branch-target
git stash pop

This workflow helps when you intentionally staged only part of a change and want to keep commit boundaries clean. It is also useful when hotfix and feature edits overlap in the same working tree during release pressure and fast patch cycles in production environments.

Common Pitfalls

A common mistake is committing first and then forgetting to move history, which requires extra cleanup commands.

Another issue is using git reset --hard without confirming whether commits were already pushed. This can disrupt collaborators.

A third issue is stashing with untracked files omitted by default when needed files are new. Use -u to include untracked paths.

Summary

  • Use git switch -c to create a new branch and keep uncommitted changes.
  • Commit work on the new branch after switching.
  • If wrong commits exist, branch then reset only when safe.
  • Use stash flow when direct switching is blocked.
  • Verify current branch before commit to avoid repeat errors.

Course illustration
Course illustration

All Rights Reserved.