To avoid downtime caused by submitting troublesome code directly to the main branch on GitHub, you should build a separate branch and work there. However, before you can work there, you must first switch to it.
Switching Branches on the GitHub Website
To swap branches from the GitHub website, open your favorite browser, navigate to GitHub’s official website, log in to your account, and then pick the repository where your branch is located.
When you first enter the repository, you’ll see a button next to the Branches and Tags choices. To open a drop-down menu, click this button. Select the desired branch from the list in the “Branches” tab. You’ll be in that branch after you’ve chosen it.
This technique is good if you’re intending to make changes to the website’s branch, but if you’re working on your local machine, you’ll want to use git.
Using git checkout to switch between branches
When working locally, you may swap branches with a single command. To get started, launch a command terminal of your choosing (for example, Terminal on Mac, Windows Terminal, or the Linux terminal). You may also use a command terminal in a text editor that supports it, such as VSCode.
Once you’re at the terminal, change directories to the repository’s location. This isn’t a one-size-fits-all command because everyone’s repository may be located in a different location on their local system.
Assume we’re at the top directory in the console, and our repo, how-to-switch, is located in the file path OneDrive > Desktop > _GIT. We’d execute the following command:
Now that you’re in the right directory, you may swap branches. To change branches, use the following command:
git checkout *branch name*
So, if your branch is called “test-branch,” you would run:
git checkout test-branch
You have successfully changed branches.
Using git switch to switch between branches
You may also swap branches with the git switch command. To begin, open your selected terminal and use the cd command to get to the correct directory. Once in the correct directory, execute the following command:
git switch *branch*
So, if our branch name is test-branch, we’d execute the following command:
git switch test-branch
You have now successfully changed branches.
The difference between git switch and git checkout
Git checkout and git switch may appear to accomplish the same operation under different labels at first look. You’re not far off the mark, but there is one little distinction you should be aware of. Git checkout offers more than simply branch switching, which is why we built git switch–to clear up any confusion.
git switch merely creates a new branch. That’s all. However, git checkout performs three functions: it swaps branches, but it also moves files from the stage and a tree-ish to the working tree.
Working on distinct branches (as opposed to the main branch) reduces production downtime caused by bad code being submitted to production. Testing your code on a child branch will save you a lot of time and effort. And, once your branch has been merged into the main branch, remember to remove it to protect the repository from becoming crowded.