Skip to Content

Git switch – a replacement for stash if on wrong branch

Created on
Updated on
Approx ~2 minutes reading time for 317 words.

Introduction

Ever done some work, saved a file (or files) and then realised you’re on the wrong branch? We’ve all been there, and up until I found out about this switch command I’d always had to use stash to get myself out of trouble.

Say hello to switch, my new friend

So you’ve added or amended files (but not committed yet) what you can do is either switch to an existing branch;

git switch feature/this-branch-exisits

Or use the -c flag to ‘create’ the branch, similar to git checkout -b like so;

git switch -c feature/this-is-a-newly-created-branch

You’re now ready to carry on or start doing Git commits (in the correct branch). Sweetness!

Final thoughts

I love this feature and whilst switch has been available since Git 2.23 I was not aware of it. If you follow the link you can also read up on the restore command which was implemented at the same time.

// End of Article

Article Information

Category: QuickNote
Topic: #Tech-Stack

Dave A.K.A. 'barrd'

Dave is a Bristol based Scottish Expat who has 20+ years experience of web development. Loves playing guitar, reading books, watching Sci-Fi and tinkering with tech.

About Dave A.K.A. 'barrd'

Image for Dave A.K.A. 'barrd'
Dave is a Bristol based Scottish Expat who has 20+ years experience of web development. Loves playing guitar, reading books, watching Sci-Fi and tinkering with tech.

Read more about Dave