Git users use branches in the git repository to keep the project work separate, and the users can test any task without affecting the main project by doing the task in a new branch. The users need to switch between the branches based on the project work. `git checkout` command is used to switch between the branches before. This command is used to switch between the branches and perform different types of tasks such as restore files, undo changes, etc. Another command, `git switch,` is added to switch between the branches in the new version of git. It is simpler than the `git checkout` command, and it is used for creating and switching branches only. How these two commands can be used to switch between the branches have shown in this example.

Prerequisites:

Install GitHub Desktop

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a local repository

You have to create a local repository to test commands used in this tutorial to switch between the branches.

Switch branch using `git checkout`:

You can create a new local git repository or use any existing local git repository to test the commands used in this part of the tutorial. I have used an existing local repository here named upload-file. Open the terminal and go to the repository folder.

Run the following command to check the current branch list and switch to the branch named multiple using the `git checkout` command.

$ git branch


$ git checkout multiple

The following output will appear after executing the above commands if the branch exists.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-switch-branch-in-Git-1.png" data-lazy- height="140" src="data:image/svg xml,” width=”770″>

-b option is used with the `git checkout` to create a new branch and switch to the new branch. Run the following `git checkout` command to create a new branch named newBranch and switch to the new branch. Next, the `git branch` command will display the existing branch list of the repository.

$ git checkout -b newBranch


$ git branch

The following output will appear after executing the above commands. The current active branch is the newly created branch here.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-switch-branch-in-Git-2.png" data-lazy- height="153" src="data:image/svg xml,” width=”771″>

A new branch can be created with the content of any existing branch by using the `git checkout` command. Run the following command to create a new branch named testBranch from the existing branch, multiple. Next, the branch list will be printed to check that the new branch is created and switch to the newly created branch.

$ git checkout -b testBranch multiple


$ git branch

The following output will appear after executing the above commands. The current active branch is the newly created branch here.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-switch-branch-in-Git-3.png" data-lazy- height="171" src="data:image/svg xml,” width=”859″>

Switch branch using `git switch`:

Another way to switch branches has shown in this part of the tutorial. The same local repository has been used to check the `git switch` command for switching the branch. The following commands will check the existing list of the branch with an active branch and switch to the newBranch.

$ git branch


$ git switch newBranch

The following output will appear after executing the above commands. The active branch was testBranch, and the current active branch is newBranch after executing the `git switch` command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-switch-branch-in-Git-4.png" data-lazy- height="274" src="data:image/svg xml,” width=”811″>

Like the `git checkout` command, the `git switch` command can also create a new branch and switch to the new branch using the -c option. Run the following `git switch` command to create a new branch named master and switch to the branch. Next, the `git branch` command is used to check the current branch list and which branch is active now.

$ git switch -c master


$ git branch

The following output will appear after executing the above commands. The master branch has been created, and this branch is active now.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-switch-branch-in-Git-5.png" data-lazy- height="186" src="data:image/svg xml,” width=”838″>

You can undo the changes made in the branch at the time of switching the branch. To test this feature, run the following commands to create a text file and add the file to the master branch of the repository.

$ cat > testfile.txt


$ git status


$ git add testfile.txt

The following output will appear after executing the above commands. Here, the text file has been added but not committed yet.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-switch-branch-in-Git-6.png" data-lazy- height="205" src="data:image/svg xml,” width=”846″>

Now, make the main as an active branch and run the following commands to switch to the master branch by omitting the changes made in that branch before.

$ git branch


$ git switch master –discard-changes


$ git status

The following output will appear after executing the above commands. The output shows that the active branch was main, and the current branch has switched to the master branch after discarding all changes of this branch. That means that the text file that was added in the previous command has been removed from the branch before the switch.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-switch-branch-in-Git-7.png" data-lazy- height="247" src="data:image/svg xml,” width=”866″>

Another feature of the switch command is that you can switch to the active branch before easily by using the `git switch – ` command. Run the following commands to check which branch is active now and switch to the previously active branch.

$ git branch


$ git switch –

The following output will appear after executing the above commands. The output shows that the active branch was master and the active branch switch to the main branch that was the previous active branch.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-switch-branch-in-Git-8.png" data-lazy- height="202" src="data:image/svg xml,” width=”847″>

The above tasks that have been shown by executing commands from the terminal can be done very easily by using the GitHub Desktop application. Open the repository in this application and click the Branch list from the View menu to display the existing branch list with the active branch. You can easily switch to any branch from the list by clicking on that branch name.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-switch-branch-in-Git-9.png" data-lazy- height="485" src="data:image/svg xml,” width=”924″>

Conclusion:

Different ways to switch branch in git have been shown in this tutorial by using two `git checkout` and `git switch` commands in a demo repository. The git users can use any of these commands to switch branch of the repository.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/channel-logo-150×150.jpg609fd20a7040e.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.