git
version control
branching
software development
git-commands

Moving uncommitted changes to a new branch

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A common Git workflow mistake is making uncommitted changes on one branch, then realizing those edits belong on a different feature branch. The good news is that Git can move this work cleanly without losing edits if you use the right sequence. The best approach depends on whether branch switching is immediately possible or blocked by conflicts.

Quickest Path: Create New Branch from Current State

If your current working changes do not conflict with branch checkout rules, the simplest command is:

bash
git switch -c feature/new-task

Older equivalent:

bash
git checkout -b feature/new-task

This creates a new branch at current commit and keeps your uncommitted changes in working tree and index.

Validate immediately:

bash
git branch --show-current
git status

This confirms your work is now attached to the new branch context.

When Switching Is Blocked

Sometimes Git blocks checkout because changes would be overwritten by files in target branch. In that case, stash first.

bash
git stash push -m "wip move to new branch"
git switch -c feature/new-task
git stash pop

If conflicts appear during stash pop, resolve and continue with normal staging and commit.

Preserve Untracked Files Too

By default, stash may skip untracked files. If you created new files that need to move with your work:

bash
git stash push -u -m "wip including untracked"

Then branch-switch and pop as usual.

Move Only Part of Your Changes

Sometimes only subset of edits belongs to new branch. Split changes using interactive staging.

bash
git switch -c feature/new-task
git add -p
git commit -m "Move relevant hunk set"

Remaining unstaged changes can be stashed or committed separately. This keeps branch history focused and review-friendly.

Scenario: You Already Committed on Wrong Branch

If mistake is discovered after commit:

  1. create new branch at current commit
  2. move original branch pointer back if needed
bash
1git switch -c feature/new-task
2
3# if you need to undo commit on previous branch:
4git switch original-branch
5git reset --hard HEAD~1

Use hard reset only when you are certain no shared history depends on that commit.

Safe Recovery with Reflog

If you make a wrong move, reflog can usually recover branch heads.

bash
git reflog
# identify previous state and reset or checkout it

Keeping this recovery option in mind makes branch correction less risky.

Push and Track the New Branch

After you commit moved changes, publish with upstream tracking.

bash
git push -u origin feature/new-task

This enables simpler future git push and git pull commands.

Team Workflow Best Practices

To avoid repeated branch-move corrections:

  • create branch before first code change
  • use issue-based branch names
  • keep commits small and focused
  • run git status and git branch --show-current before committing

These habits reduce cleanup work and improve pull request clarity.

Practical Decision Guide

Use this quick decision tree:

  • no checkout conflict and all work should move: git switch -c
  • checkout conflict present: stash then branch
  • only partial work should move: branch then git add -p
  • already committed on wrong branch: create new branch and reset old branch carefully

Selecting based on situation prevents data loss and messy history.

Common Pitfalls

  • Continuing to commit on wrong branch after noticing the mistake.
  • Using hard reset without confirming commit safety.
  • Forgetting untracked files when stashing work for branch migration.
  • Moving all changes when only a subset belongs to new task.
  • Skipping verification of active branch before commit.

Summary

  • Uncommitted changes can usually be moved safely by creating a new branch directly.
  • Stash is the fallback when checkout is blocked by file conflicts.
  • Interactive staging helps split mixed work into correct branches.
  • Reflog provides recovery safety if branch operations go wrong.
  • Early branch discipline is the best prevention for misfiled local changes.

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.