Using Git Switch To Change Branches - Tempertemper
Maybe your like
Since upgrading to macOS Big Sur and its version of Xcode Command Line Tools, I’ve noticed that git switch now works.
git checkout has always been a funny one for me; it switches branches, allows you to view code at older commits, discards changes to a file, and probably some other things I don’t know about. The Jack of all trades of Git commands.
git switch was introduced in Git version 2.23 to separate jumping from branch to branch into its own command. A checkout command would look like this:
git checkout my-featureWe can now replace that with switch:
git switch my-featureBut why is git switch a good thing?
More plain English
The main reason I like git switch is that it says what it means: switch to another branch. ‘Checking out’ another branch might make sense when we’re going back in time to have a quick look at (‘check out’), how things looked on an old commit before coming back to the present-day commit. But when when we plan to do work on a branch, we’re doing a lot more than just checking it out.
The semantics of ‘checking out’ could also be interpreted as leaving, not moving to; it’s a bit of an odd ball.
And if I’m teaching someone how to use Git, checkout has always required some explanation where switch won’t.
More sensible flags
I often like to create a new branch and ‘checkout on it’ in one move, so the following command gets a lot of use:
git checkout -b my-new-featureUsing the -b flag means ‘create a new branch that doesn’t already exist, then checkout on it’. b for ‘branch’ I guess, but that doesn’t quite make sense as we’re already doing something with a branch when we run the checkout command without the -b. So what does b stand for? Maybe ‘build’? Annoyingly, there isn’t a longhand for it, like -f has --force, so there’s no way of knowing for sure.
Happily, the switch equivalent of the -b flag makes much more sense:
git switch -c my-new-featurec for ‘create’: we’re switching to and creating a branch. And -c has a longhand equivalent that confirms this meaning:
git switch --create my-new-featureSingle purpose
I mentioned that, as well as switching branches, git checkout allows you to jump back to a previous commit using a commit hash:
git checkout abc1234You can also discard untracked changes to a file; for example, if I’ve made some changes to my homepage that I don’t want to keep, I might restore the file to its pre-edited state with:
git checkout index.htmlYou can’t do either of those things with git switch. And that’s a good thing in my eyes: one command should do one thing (which is why I also use git restore).
I’ll be using git switch to change branches in future.
Tag » How To Change Branch In Git Without Commit
-
Git: Switch Branch And Ignore Any Changes Without Committing
-
How To Change The Branch Without Committing So That I Can ... - Reddit
-
Git (revision Control): If You Do Not Commit And You Switch Branches ...
-
Move Existing, Uncommitted Work To A New Branch In Git | Baeldung
-
Git Switch Branch – How To Change The Branch In Git - FreeCodeCamp
-
Git - Git-checkout Documentation
-
Git: Switch Branch And Ignore Any Changes Without Committing
-
Switching Branch Without Commit In Working Directory - DQ Courses
-
How To Switch Branch On Git - Devconnected
-
Learn Git Collaboration - Nulab
-
Stashing Changes - GitHub Docs
-
Git Stash: Save Local Changes Without Commit In Git - Fedingo
-
Create A New Git Branch With Current Local Changes Saved By Example
-
Git Stash - How To Save Your Changes Temporarily