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?
Enter git switch
.
The git switch
command
There’s this 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
.
$ 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 featureB
branch.
Learn more about this commad — git-switch
👋 Hi there! I'm Amit. I write articles about all things web development. If you enjoy my work (the articles, the open-source projects, my general demeanour... anything really), consider leaving a tip & supporting the site. Your support is incredibly appreciated!