Quickly switch to new branch with unstaged changes in Git
Consider the scenario where you’re working on a Git branch called
featureB and you have a lot of unstaged files in this branch.
Now, for some reason, you don’t have to work on this branch anymore and switch to a new branch called
featureX (which is currently non-existent) and apply all the unstaged changes to this branch.
The usual way of doing this would be to…
- First stash the unstaged changes.
- Create and checkout to this new branch.
- Apply or pop the previous stash on this branch.
That’s quite a process to follow. But what if I tell you, there’s a command to do all of this all at once?
git switch command
git switch command using which it becomes fairly easy to switch between branches.
Especially, the scenario I mentioned previously can be done in a cinch using this command.
So, if you want to switch to the
featureX branch from
featureB, this is how you can do it using
$ git switch -c featureX
And that’s it! This command (with the option
-c which means
create) will create a new branch called
featureX, switch to it, and apply all of the unstaged changes of the
Learn more about this commad — git-switch