How to Switch Branches in GitHub

Publish date: 2024-07-04

Quick Links

To ensure there's no downtime due to pushing problematic code straight to the main branch on GitHub, you should create a new branch and work there. Before you can actually work there, though, you'll need to switch to it.

Switch Branches From the GitHub Website

To switch branches from the GitHub website, first launch your preferred browser, go to GitHub's official website, log in to your account, and then select the repository your branch is in.

Related: How (and Why) to Create a GitHub Repository

Once in the repository, you'll see a button next to the Branches and Tags options. Click this button to display a drop-down menu. In the "Branches" tab, select the desired branch from the list. Once selected, you'll then be in that branch.

This method is fine if you're going to make your changes to the branch on the website, but if you're working on your local machine, you'll want to use a

git 

command.

Switch Branches Using git checkout

If you're working locally, you can switch branches using a simple command. To get started, you'll need to open a command terminal of your choice (for example, Terminal on Mac, Windows Terminal, or the Linux terminal). Alternatively, you can use the command terminal in a text editor that supports it, such as VSCode.

Related: How to Checkout a Remote Git Branch

Once in the terminal, you'll want to change directories to the location of the repository. This isn't a one-size-fits-all command, as everyone may have their repository stored in a different directory on their local machine.

As an example, let's assume that we're in the top directory in the terminal, and our repo called

how-to-geek 

 is located in the file path OneDrive > Desktop > _GIT. We'd run this command:

cd OneDrive\Desktop\_GIT\how-to-geek

You can now switch branches now that you're in the correct directory. To switch branches, run this command:

git checkout <branch-name>

So if your branch were named "test-branch" then you would run:

git checkout test-branch

You've successfully switched branches.

Switch Branches Using git switch

You can also use the git switch command to switch branches. First, open your desired terminal and change to the proper directory using the cd command. Once in the proper directory, run this command:

git switch <branch>

So, if our branch name is test-branch then we'd run this command:

git switch test-branch

You'll now have successfully switched branches.

 git switch vs. git checkout

At first glance, git checkout and git switch may appear to do the same thing under different names. You're not far from wrong, but there is a minor difference you should make note of. git checkout provides additional functionality than just switching branches, which is why developers created git switch--to clear the confusion.

git switch only switches to a new branch. That's it. git checkout, however, does three things: it switches branches, but it also copies files from the stage and from a tree-ish to the working tree. If you're interested in learning more about this, Dan Fabulich from Redfin Engineering gives a fantastic breakdown.

Working on separate branches (that's not the main branch) prevents production downtime from poor code being pushed to production. Testing your code in a child branch will save you a world of trouble. And once you merge your branch into the main branch, don't forget to delete it to keep the repository from being cluttered.

Related: How to Delete a Branch on GitHub

ncG1vNJzZmivp6x7qbvWraagnZWge6S7zGhucXFia4VwtM6wZK2nXajEqsDCoWSbqpGjsKmx0magp2WXnsGpwcFo