

You can’t do either of those things with git switch. 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 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 And -c has a longhand equivalent that confirms this meaning: git switch -create my-new-feature Single purpose Happily, the switch equivalent of the -b flag makes much more sense: git switch -c my-new-featureĬ for ‘create’: we’re switching to and creating a branch. 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. 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.

Using the -b flag means ‘create a new branch that doesn’t already exist, then checkout on it’. 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
#GIT CHANGE BRANCH WITHOUT CHANGING FILES HOW TO#
The semantics of ‘checking out’ could also be interpreted as leaving, not moving to it’s a bit of an odd ball.Īnd if I’m teaching someone how to use Git, checkout has always required some explanation where switch won’t.

But when when we plan to do work on a branch, we’re doing a lot more than just checking it out. ‘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. The main reason I like git switch is that it says what it means: switch to another branch. We can now replace that with switch: git switch my-featureīut why is git switch a good thing? More plain English A checkout command would look like this: git checkout my-feature Git switch was introduced in Git version 2.23 to separate jumping from branch to branch into its own command.
#GIT CHANGE BRANCH WITHOUT CHANGING FILES CODE#
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. Since upgrading to macOS Big Sur and its version of Xcode Command Line Tools, I’ve noticed that git switch now works.
