How to change branch in GitHub

GitHub Heroes

To ensure that there is no downtime from submitting problematic code directly to the master branch on GitHub, you should create a new branch and work there. Before you can work on it, however, you’ll need to go through it.

Switch branch from GitHub website

To switch branches from the GitHub website, first launch your favorite browser, navigate to the official GitHub website, log in to your account, and then select the repository where your branch is located.

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

Once in the repository, you will 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 will then be in this branch.

View a list of branches.

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

Change branch using git checkout

If you’re working locally, you can switch branches with a single 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). You can also use the command terminal in a text editor that supports it, such as VSCode.

RELATED: How to Make Windows Terminal Your Default Terminal App

Once in the terminal, you’ll want to change the directories to the repository location. This is not a one-time-order, as everyone can have their repository stored in a different directory on their local machine.

For example, suppose we are in the top directory of the terminal and our repository is called how-to-geek is located in the file path OneDrive > Desktop > _GIT. We would run this command:

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

Switch to the working directory.

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

git checkout <branch-name>

So if your branch was called “test-branch”, you would run:

git checkout test-branch

Run the command to switch branches.

You have successfully changed branches.

Switch branch using git switch

You can also use the git switch command to switch branches. First, open your terminal of choice and navigate to the appropriate directory using the cd order. Once in the right directory, run this command:

git switch <branch>

So if our branch name is test-branch then we would run this command:

git switch test-branch

The new command to switch branches.

You have now 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 are not far from wrong, but there East a minor difference you should note. git checkout provides additional functionality beyond simple branch switching, which is why the developers created git switch– to clear up the confusion.

git switch just move to a new branch. That’s it. git checkout, however, does three things: it switches branches, but it also copies files from the scene and from a tree to the working tree. If you want to know more about it, Dan Fabulich of Redfin Engineering gives a fantastic breakdown.

Working on separate branches (it’s not the main branch) avoids production downtime due to bad code being pushed into production. Testing your code in a child branch will save you a world of trouble. And once you’ve merged your branch into the master branch, don’t forget to delete it to avoid cluttering the repository.

RELATED: How to delete a branch on GitHub

Leave a Comment