Skip to: Content or Footer

Git switch – a replacement for stash if on wrong branch

Created on
No updates
Approx ~2 minutes reading time for 311 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!

In conclusion

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

About Dave Barr

Scottish Expat who has 20+ years experience of Web Development and is continually on the look out to improve his skill sets. Learning new and innovative solutions for current requirements in the world of IT, Web Development and eCommerce.

Read more about Dave

Back to Top