Git

Move Existing, Uncommitted Work to a New Branch in Git

While working on a large project, each developer works on different branches. Sometimes, they start working on a new feature in the existing branch, for instance, a local “master” branch, rather than creating a new branch. Later, it is realized that they are working in the wrong branch and need a new branch for this feature. So, move all the uncommitted work to a new branch and keep the “master” branch unchanged.

This post will talk about the method of moving the exciting, uncommitted work to a new Git branch.

How to Move Uncommitted Work to a New Git Branch?

To move the existing, uncommitted work to a new branch, execute the “$ git checkout -b <new-branch-name>” or “$ git switch -c <new-branch-name>” commands. These commands create a new branch and switch to it while leaving the current branch as it is and bringing all uncommitted changes to the new branch. Follow the provided steps to do this:

  • Move to the local repository.
  • Generate a new file.
  • Check current status.
  • Make a new branch and checkout to it.
  • View the status of the new branch.
  • Stage and commit changes.
  • Verify new changes.
  • Switch back to the previous branch and check the current status.

Step 1: Go to the Repository
First, navigate to the Git repository using “cd <“repository-path”>” command:

$ cd "C:\Git\test_repo"

Step 2: Check Git Branches
Then, view the list of available Git branches in the repository:

$ git branch

Here you can see, two branches “alpha” and “master” are available, and the asterisk “*” symbol beside the “master” branch indicates it is current working branch:

Step 3: Create a New File
To generate a new file in the current working branch, execute the “$ touch” command:

$ touch test_1.txt

Step 4: Check Status
After that, check the current status of the branch by executing the below-given command:

$ git status

It can be observed that the new “test_1” file is unstaged:

Step 5: Create and Switch Branch
To create a branch and switch to it automatically, utilize the provided command:

$ git checkout -b feature1

In the below image, it can be seen that a new branch name “feature1” has been created and switched successfully:

Another way to generate a new branch and navigate to it immediately is to use the “$ git switch -c <new-branch-name>” command:

$ git switch -c feature2

Step 6: View Newly Created Branch Status
Next, utilize the “$ git status” command to check the status of the Git working repository with the newly created branch:

$ git status

As you can see, the “test_1” file needs to be tracked and committed:

Step 7: Stage Changes
Then, track the working area changes to the staging index by running the “git add” command:

$ git add test_1.txt

Step 8: Commit Changes
Commit the new changes in the current working branch with the help of the “$ git commit” command along with the desired commit message:

$ git commit -m "test_1 file added"

Step 9: Verify Committed Changes
To verify the committed changes, run the following command:

$ git status

As you can see, there is nothing to commit, and new changes have been added to the newly created branch:

Step 10: Switch to “master” Branch
Execute the below-provided command after completing the task in the current working branch to switch back to the “master” branch:

$ git checkout master

Step 11: Verify Status
Lastly, check the status of the current working branch:

$ git status

This below-provided output indicates that there is no new commit and local change on the master branch:

We have efficiently explained the methods of moving existing, uncommitted work to the new branch in Git.

Conclusion

To move existing uncommitted work to the new branch in Git, utilize the “$ git checkout -b <new-branch-name>” or “$ git switch -c <new-branch-name>” commands. These commands create a new branch and immediately move the uncommitted changes to it by switching. This article demonstrated the procedure of moving existing, uncommitted work to the new branch in Git.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.