Skip to main content

Using Git switch to change branches

First posted in Development and Git; updated 13th April 2021

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-feature

We can now replace that with switch:

git switch my-feature

But 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-feature

Using 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-feature

c 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-feature

Single 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 abc1234

You 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.html

You 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.

Accessibility in your inbox

I send an accessibility-centric newsletter on the last day of every month, containing:

  • A roundup of the articles I’ve posted
  • A hot pick from my archives
  • Some interesting posts from around the web

I don’t collect any data on when, where or if people open the emails I send them. Your email will only be used to send you newsletters and will never be passed on. You can unsubscribe at any time.

More posts

Here are a couple more posts for you to enjoy. If that’s not enough, have a look at the full list.

  1. Not all screen reader users are blind

    There’s a common misconception that everyone who uses screen reader software is blind; that’s mostly the case, but not always.

  2. Accessibility by degrees

    Retro-fitting accessibility is far from ideal but usually the only way digital products are able to reach all of their potential users.