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 checkout multiple
The following output will appear after executing the above commands if the branch exists.
-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 branch
The following output will appear after executing the above commands. The current active branch is the newly created branch here.
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 branch
The following output will appear after executing the above commands. The current active branch is the newly created branch here.
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 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.
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 branch
The following output will appear after executing the above commands. The master branch has been created, and this branch is active now.
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.
$ 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.
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 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.
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 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.
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.
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.