Home » Git » How to switch or change branch in git

How to switch or change branch in git

By Emily

If you’re fairly new to git and you’ve found this post, you probably know how to clone a git repo, and now you’re wondering how to switch branches. When you clone a repo you’ll generally start out on the main or master branch. Before you start coding you will probably want to ‘git change branch‘. At least that might be what you’re searching for, but it isn’t really a command in git at all. What you actually need to change branch in git is either git switch or git checkout. They are two different commands you can use to change branch, they do exactly the same thing, so I’ll cover both – git switch and git checkout. Git checkout also has some other functionality which I’ll also explain.

How to git change branch

As mentioned there are two commands which you can use to switch which branch you are on.

Use git checkout to switch branch

This is the original command to ‘git change branch‘, and the one most people are familiar with. You use the checkout command like this, to change from your current branch to a branch which is named my-branch:

git checkout my-branch

After running that command, if you were to do a git status, you’ll see your current branch is now my-branch, so you have changed branches from main to my-branch.

Use git switch to change branch

This is the newer of the two commands, and the one I use because it makes much more sense when you’re typing it and just feels more intuitive. You are changing or switching branches.

git switch my-branch

In the official documentation for git switch you can see that this command was added in git 2.23, when the developers of git decided to split some of the functionality that git checkout previously offered, into two clearer uses. More on this below.

Is git checkout the same as git switch?

Git checkout does more than git switch, so no, but also yes :). If you compare the following two commands, then yes they do do exactly the same thing:

git checkout branch-name
git switch branch-name

This is all that git switch does, it is only used to switch which branch you are on. But git checkout does this and more…..

What does git checkout do?

The command git checkout has two uses, one to change which branch you’re on where it’s used like this (already covered in this post):

git checkout branchName

… and it’s other use is to ‘checkout’ a file, hence it’s name I guess. But what does ‘checkout a file’ actually mean.

It means to discard your local changes to a file. So if you’ve been working on a file called newPage.jsx and you decide you’ve made a mess of it and want to start again, then by doing:

git checkout newPage.jsx

… your local branch will revert to the last committed version of the newPage.jsx file.

What is the git switch -c command?

If you use the -c flag with the git switch command then you will create a new branch and switch to it in one command. So this:

git switch -c <new-branch>

… is the same as….

git branch <new-branch>
git switch <new-branch>

How to switch to master branch

So if you wanted to switch to master branch in git you would type:

git switch master

It’s that simple to switch branches in git!

FAQs

How to change branch in git?

Use git switch my-branch-name or git checkout my-branch-name to change which branch you are on in git

What does git checkout -b branchname do?

Using the -b flag in the git checkout command to instantly switch to the new branch.

You may also be interested in: