Create Git branch with current changes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Starting work on the wrong branch is common, especially during fast context switching. The good news is that Git can move your current work to a new branch without losing staged or unstaged edits. The correct method depends on whether changes are uncommitted, committed locally, or already pushed.
Create a Branch and Keep Current Uncommitted Work
If files are modified but not committed, the simplest command is creating a branch at current HEAD.
This moves your branch context, not your file content. Your working tree and index remain intact, so changed files follow you to the new branch automatically.
Verify immediately:
This quick check prevents accidental commits on the original branch.
Understand What Happens to Staged Files
Staged changes are also preserved when you create a new branch from your current location. That means files added with git add stay staged and ready to commit.
This behavior is useful when you already prepared part of the commit and only need to correct branch location.
Handle Switch Conflicts With Stash
Sometimes Git blocks branch switching because your local edits would be overwritten by files from target history. In that case, stash changes, create the branch, then reapply.
Use -u when untracked files are part of your work. After stash pop, inspect conflicts and resolve them before committing.
Move Already Committed Work Off the Wrong Branch
If you already committed on the wrong branch but have not pushed, create the right branch at that commit first. Then return to the wrong branch and clean it.
This leaves the commit on the new feature branch and removes it from local main. Use this only when the wrong-branch commit is still private.
If you already pushed the wrong commit to a shared branch, do not rewrite shared history casually. Prefer git revert on the shared branch.
This keeps collaboration safe and auditable.
Split Mixed Changes Before Final Commit
When current edits contain multiple concerns, move to the correct branch first, then create focused commits with patch staging. This improves review quality.
Smaller commits reduce rollback risk and make root-cause analysis easier during incidents.
Publish Branch With Tracking
After cleanup and commits, publish branch and set upstream tracking.
-u saves the remote tracking configuration so future pushes and pulls are straightforward. In pull request workflows, open the PR early so teammates see branch intent and avoid duplicated work.
Practical Workflow to Prevent Wrong-Branch Work
A repeatable routine prevents this problem from recurring.
- Check current branch before first edit.
- If wrong, create the correct branch immediately.
- Keep branch names task-specific.
- Commit in small, topic-focused units.
- Push branch early and open PR for visibility.
You can also add shell prompt branch display or Git aliases to reduce branch mistakes during fast development cycles.
Common Pitfalls
- Assuming branch creation duplicates commits or files. It only creates a new pointer.
- Using
reset --hardon shared branches to remove wrong commits. - Forgetting
-uon first push and then fighting tracking errors later. - Stashing without message and losing context when multiple stashes exist.
- Committing unrelated changes together after moving branches.
Summary
- Use
git switch -cto move current uncommitted work to a new branch safely. - Staged changes also follow branch creation from current
HEAD. - If switching is blocked, stash with
-u, switch, and pop. - For wrong-branch commits, choose reset for private history and revert for shared history.
- Use patch staging to split mixed work into reviewable commits.
- Push with upstream tracking and open PR early for team clarity.

