Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

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

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?