How to moving existing uncommitted work to a new branch in git?
When working with Git, you might find yourself making changes on the wrong branch or decide that your modifications should be on a new branch to keep the main branch clean or for other reasons. Fortunately, Git offers flexibility to move uncommitted changes to a new branch. Here are the steps:Check current changes:Before moving the changes, check the status of your working directory and staging area. You can use the following command:This will display the status of your current changes, whether they are staged or unstaged.Create and switch to a new branch:If you have staged the changes, unstage them first (if you intend to move them to the new branch). Then, create and switch to a new branch using the following command:This command creates a new branch named and switches to it.Add and commit changes:Now that you are on the new branch, add and commit your changes. Use the following command to stage all changes:Or, if you want to add specific files, use:Next, commit the changes to your new branch:(Optional) Keep the main branch clean:If you just created the new branch from the main branch (e.g., or ) and don't want these changes to appear on the main branch, switch back to the main branch and discard these changes. First, switch back to the main branch:Then, use the following command to discard uncommitted changes:This will reset the main branch to the last commit state, discarding all uncommitted changes. Note that this is a dangerous operation as it discards all uncommitted changes. Before using , ensure you don't need these uncommitted changes.This is the basic process for moving uncommitted work to a new branch. Let's look at a specific example:Suppose you are working on the main branch and have made some changes. Now you want to move these changes to a new branch .Check changes:Create and switch to new branch :Stage all changes and commit them to the new branch:If needed, switch back to branch and discard changes:Now, the new branch contains the previously uncommitted work, while the branch remains unchanged.