Git switch – a replacement for stash if on wrong branch
Approx ~2 minutes reading time for 311 words.
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!
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
- Git Stash (git-scm.com)
- Highlights from Git 2.23 (github.blog)