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:
Step 2: Check Git Branches
Then, view the list of available Git branches in the repository:
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:
Step 4: Check Status
After that, check the current status of the branch by executing the below-given command:
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:
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:
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:
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:
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:
Step 9: Verify Committed Changes
To verify the committed changes, run the following command:
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:
Step 11: Verify Status
Lastly, check the status of the current working branch:
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.