Git Create New Branch With Current Changes

6 min read

When working with Git, developers frequently encounter a scenario where they have started coding on the wrong branch—usually main or `master*—only to realize halfway through that these modifications belong on a dedicated feature branch. And fortunately, Git provides several straightforward methods to create a new branch with current changes without losing a single line of code. Understanding these workflows is essential for maintaining a clean commit history and avoiding the frustration of manual file copying or stashing conflicts.

You'll probably want to bookmark this section.

Why Move Changes to a New Branch?

Before diving into the commands, it helps to understand why this situation arises. If you leave changes uncommitted, switching branches becomes risky because Git prevents switching if the changes would be overwritten by the target branch. Practically speaking, if you commit directly to main, you pollute the production-ready history. Even so, it is easy to forget to run git switch -c new-feature before writing code. Git’s branching model is lightweight, encouraging developers to isolate work for specific features, bug fixes, or experiments. Moving uncommitted or committed work to a fresh branch keeps the main line stable and organizes your repository logically Simple, but easy to overlook. Turns out it matters..

Scenario 1: Uncommitted Changes (The Most Common Workflow)

This is the standard "oops, I'm on main" moment. Think about it: you have edited files, perhaps staged some with git add, but haven't committed yet. The safest and most modern way to handle this is using git switch.

Using git switch -c (Recommended)

Introduced in Git 2.23, git switch is designed specifically for changing branches, separating the concerns of switching branches from restoring files (which git checkout handles both) No workaround needed..

  1. Verify your status: git status. You will see your modified files listed under "Changes not staged for commit" or "Changes to be staged for commit."
  2. Create and switch simultaneously:
    git switch -c 
    
    The -c flag stands for "create."
  3. Verify: Run git status again. You are now on <new-branch-name>, and all your modifications—staged and unstaged—have traveled with you automatically.

Why this works: Git carries the working tree and index (staging area) state over to the new branch because the new branch points to the exact same commit (HEAD) you were just on. No history rewriting occurs; you simply move the "pointer" to a new label Worth keeping that in mind..

Using git checkout -b (Legacy but Ubiquitous)

If you are on an older Git version (pre-2.23) or muscle memory dictates, the classic command remains fully functional:

git checkout -b 

Functionally, this is identical to git switch -c for this specific use case. It creates the branch at the current commit and checks it out, preserving all working directory changes Most people skip this — try not to..

Scenario 2: Changes Already Committed to the Wrong Branch

Sometimes you don't realize the mistake until after you have run git commit. Your history now looks like: A -- B -- C (main) [HEAD], where C is the commit that should have been on a feature branch Easy to understand, harder to ignore..

The "Reset and Switch" Method

This approach rewinds the current branch (main) to the state before your commits, then places those commits on a new branch. Only do this if you have not pushed main to a remote shared repository. Rewriting public history causes severe collaboration issues.

Counterintuitive, but true.

  1. Create the new branch at the current position (keeping the commits):
    git branch 
    
    Now <new-branch-name> points to commit C.
  2. Reset main back to the previous commit (usually origin/main or HEAD~1):
    git reset --hard HEAD~1
    
    Use --hard to discard the changes from the working directory and index on main, matching the remote state. Use --soft if you want to keep the file changes as uncommitted modifications on main (less common for this workflow).
  3. Switch to your new branch:
    git switch 
    

Your history is now clean: main points to B, and <new-branch-name> points to C.

The "Cherry-pick" Alternative (If Main Was Pushed)

If you have already pushed the bad commit to origin/main, you cannot safely reset main locally without force-pushing, which disrupts teammates. Instead, create the branch, then revert the commit on main.

  1. Create branch from current HEAD:
    git branch 
    
  2. Switch back to main:
    git switch main
    
  3. Revert the unwanted commit (creates a new "undo" commit):
    git revert HEAD
    
  4. Switch to your feature branch to continue working:
    git switch 
    

This preserves history integrity on the shared remote while isolating your feature work.

Scenario 3: Using git stash as an Intermediate Step

While git switch -c handles uncommitted changes natively, git stash remains a powerful mental model for "saving work temporarily." Some developers prefer this explicit workflow, especially if they need to pull updates from origin/main before creating the branch.

  1. Stash your changes:
    git stash push -m "WIP: feature X changes"
    
    This cleans your working directory, reverting tracked files to HEAD.
  2. (Optional) Update main: git pull origin main.
  3. Create and switch to the new branch:
    git switch -c 
    
  4. Reapply stashed changes:
    git stash pop
    
    Note: pop applies the stash and removes it from the stash list. Use git stash apply if you want to keep the stash as a backup.

This method is slightly more verbose but offers a safety net: if the branch creation or pull goes wrong, your code sits safely in the stash stack Still holds up..

Handling Untracked Files and Ignored Files

A common pitfall involves untracked files (new files never added to Git). Day to day, by default, git switch -c and git checkout -b do carry untracked files over because they exist in the working directory, not the index. They simply remain as untracked files on the new branch Worth knowing..

Even so, ignored files (matching .gitignore) are also left alone in the working directory. They are not touched by Git during branch switching.

Crucial Exception: If the target branch (the one you are switching to) already exists and has a file with the same name as your untracked file, Git will abort the switch with an error: The following untracked working tree files would be overwritten by checkout: .... Since you are creating a new branch (-c/-b), this conflict cannot happen because the new branch has no files distinct from the current commit.

Verifying the Result

After executing any of the above methods, always verify the state:

  1. Check current branch: git branch (look for the * asterisk) or git status (shows "On branch <name>").
  2. Inspect changes: git status should list your modifications exactly as they were before.
  3. Check commit history: git log --oneline -5 to ensure the commit structure matches your intention (e.g., the wrong

e.But , the wrong base commit is not used). Even so, g. You should see the same commit history as your previous branch, plus any new commits you make on the new branch.

Conclusion

Mastering the git switch -c command is a fundamental skill for efficient Git workflows. By understanding its native handling of uncommitted changes, you can preserve your progress without the overhead of stashing. Think about it: the git stash method remains a valuable alternative for explicit control, especially when coordinating with remote updates. Here's the thing — always verify your branch state and commit history to ensure your work is isolated correctly. With these techniques, you can confidently branch out for new features, bug fixes, or experiments, keeping your repository history clean and your development process streamlined It's one of those things that adds up..

This Week's New Stuff

Fresh from the Desk

If You're Into This

A Natural Next Step

Thank you for reading about Git Create New Branch With Current Changes. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home