Move Existing, Uncommitted Work to a New Branch in Git

Better Stack Team
Updated on June 24, 2024

To move existing, uncommitted work to a new branch in Git, you can follow these steps:

Step 1: Check Uncommitted Changes

First, make sure you have uncommitted changes in your working directory. You can check the status of your working directory using:

 
git status

Step 2: Create a New Branch

Create a new branch to move your uncommitted changes to. You can use the following command to create a new branch:

 
git checkout -b <new-branch-name>

Replace <new-branch-name> with the name of your new branch.

Step 3: Commit Changes to the New Branch

Commit your changes to the new branch using:

 
git add .
git commit -m "Your commit message"

Step 4: Switch Back to the Original Branch (Optional)

If you want to switch back to the original branch, you can do so using:

 
git checkout <original-branch-name>

Note:

  • Be careful when creating a new branch and committing changes. Ensure that the changes you're committing belong to the new branch and are not intended for the original branch.
  • If you have staged changes that you don't want to commit to the new branch, you can unstage them using git reset HEAD <file> before committing.
  • If you haven't staged all your changes and want to include them in the new branch, you can use git stash to temporarily store them and then apply them in the new branch after switching.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github