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.