Frequently find yourself working on the wrong Git branch? Whether it's accidentally modifying master
or main
, or continuing work on an outdated branch, switching unstaged changes to a new branch is a common task. Here are several approaches:
My usual workflow involves:
master
branch.master
.master
.Using the Git CLI:
This is how I typically handle unstaged changes using the command line:
git status git stash --include-untracked git checkout master git pull git branch new-feature-branch git checkout new-feature-branch git stash pop
Using Git Tower (or similar GUI):
While you can replicate the CLI steps in Git Tower, a simpler method is to create the new branch and directly switch to it using the GUI's interface. Many other Git GUIs offer similar streamlined workflows.
A More Efficient Method:
Recently, I discovered a more efficient command:
git switch -c new-branch-name
This single command creates a new branch (new-branch-name
) and switches to it, handling unstaged changes seamlessly. See the documentation for more details. (Link to documentation omitted as it's not provided in the input).
For more advanced Git tips and tricks, check out our "Advanced Git" series.
The above is the detailed content of Switching Unstaged Changes to a New Branch. For more information, please follow other related articles on the PHP Chinese website!