How to Switch Branches in Git

The development of long-term projects is never straightforward; sometimes, a team might need to test a new feature, work on an update, or make some subtle tweaks. Branching is a core aspect of using Git that allows developers to experiment without breaking the entire project. In this article, you’ll learn how to switch branches in Git.

What is Branching in Git? 

Branching in Git is the process of creating an independent line of development that is connected to a source project. Git branches allow you to have multiple isolated lines of development that do not interfere with the main program. The default and primary branch used in Git is the main or master branch; you can create many other branches from that branch which can be merged into the master when you’re satisfied with the work you’ve done.

To create a new branch on Git, you need to run the git branch command followed by the name you want to give the new branch; for instance:

$ git branch new-branch

The above example creates a new branch called ‘new-branch’ from the main branch. You will create the new branch at the exact commit point you have just branched off.

You can check the branches you have in a repository by running the git branch command without putting a name in front of it. The command line will display a list of branches; an asterisk will be in front of your active branch.

Switching Branches In Git

The ability to create branches in Git gives you lots of versatility and control over the flow of your work and the freedom to try new things. You can work on multiple branches simultaneously without affecting one another, which can be done by switching between branches. When you switch to a particular branch, every new commit will only reflect on that branch.

There are a couple of ways to switch branches in Git.

Switch Branch Using The Switch Command

The first means of switching branches on Git is through the switch command. You can use the switch command by typing git switch followed by the name of the branch that you want to switch to; for instance:

$ git switch new-branch

The above command will make the ‘new-branch’ branch the active branch with which you’re working. You have to make sure that the branch name you’re putting in front of the git switch command exists; if not, you will receive the following error message:

fatal: invalid reference: non-existing-branch

To prevent the above error, you can simultaneously create and switch to a branch that doesn’t exist by adding the -c flag and the new branch name to the git switch command. For instance:

$ git switch -c new-branch

You should get a success message saying Switched to a new branch 'new-branch'; this confirms that you have created the new branch and switched to it.

Switch Branch Using The Checkout Command

The git checkout command is a versatile Git command that you can also use to switch git branches. You can use the checkout command by typing git checkout, followed by the name of the branch to which you want to switch. For instance, here’s how we’ll switch from the new-branch branch to the main branch:

(new-branch)$ git checkout main

You will receive a message saying Switched to branch 'main'indicating that you have switched to the specified branch.

As with using the git switch command, you have to ensure that the branch you want to switch to exists; if not, you will get the following error message:

error: pathspec 'new-branch' did not match any file(s) known to git

Alternatively, you can choose to simultaneously create and switch to a new branch using git checkout; in this case, you will need to put the -b flag and the new branch name in front of the command. An example is this:

$ git checkout -b another-branch

The above command will create a new branch called ‘another-branch,’ which will be the new active branch. 

If you want to return to the last branch you worked on; you can do that by using the following command:

$ git checkout -

Checkout To Specific Commit

Whenever you use the git checkout command to switch to or create a new branch, you start from the most recent commit. However, you can choose to checkout to a specific commit and even create a new branch.

You will put the commit SHA in front of the git checkout command instead of a branch name to checkout to a specific commit. The SHA of the commit is the hash of the commit information and is unique for every commit. 

You can find out the SHA of a commit by switching to the branch where you made the commit. Once you’re in the specified branch, you should use the git log command. You will find the SHA of each commit located after the ‘commit’ message, like this:

commit 36ab1fe04727b7777f8ba0084e00a9ec808d6d39

Once you have the commit SHA, you should paste it in front of the git checkout command like this:

$ git checkout 36ab1fe04727b7777f8ba0084e00a9ec808d6d39

The message you will get after executing this command is as follows:

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

The detached HEAD state mentioned above is an experimental version of the current branch. Changes made in the detached HEAD state will be lost if you don’t save them by creating a new branch. 

Read More: How To Clone A Git Repository

Leave a Comment