Git Switch Branch – How to Change the Branch in Git

Switching branches is something you ‘ll need to do much in Git. To do this, you can use the git checkout command .

How to create a new branch in Git

To create a new arm in Git, you use the git checkout command and pass the -b flag with a name. This will create a newfangled outgrowth off of the current arm. The modern branch ‘s history will start at the current place of the arm you “ branched off of. ”

Assuming you are presently on a arm called master :

(master)$ git checkout -b my-feature
Switched to a new branch 'my-feature'

here you can see a new arm created called my-feature which was branched off of master .

How to switch to an existing branch in Git

To switch to an existing branch, you can use git checkout again ( without the -b flag ) and pass the name of the branch you want to switch to :

(my-feature)$ git checkout master
Switched to branch 'master'

There is besides a handy shortcut for returning to the previous arm you were on by passing - to git checkout rather of a branch name :

(my-feature)$ git checkout -
Switched to branch 'master'
(master)$ git checkout -
Switched to branch 'my-feature'

How to checkout a specific commit

To checkout or switch to a particular entrust, you can besides use git checkout and pass the SHA of the commit rather of a branch identify. After all, branches are truly just pointers and trackers of specific commits in the Git history .

How to find a commit SHA

One way to find the SHA of a entrust is to view the Git logarithm. You can view the log by using the git log control :

(my-feature)$ git log
commit 94ab1fe28727b7f8b683a0084e00a9ec808d6d39 (HEAD -> my-feature, master)
Author: John Mosesman 
Date:   Mon Apr 12 10:31:11 2021 -0500

    This is the second commmit message.

commit 035a128d2e66eb9fe3032036b3415e60c728f692 (blah)
Author: John Mosesman 
Date:   Mon Apr 12 10:31:05 2021 -0500

    This is the first commmit message.

On the first line of each commit after the parole commit is a long string of characters and numbers : 94ab1fe28727...

This is called the SHA. A SHA is a unique identifier that is generated for each invest. To checkout a specific perpetrate, you equitable need to pass the invest ‘s SHA as the argument to git checkout :

(my-feature)$ git checkout 035a128d2e66eb9fe3032036b3415e60c728f692
Note: switching to '035a128d2e66eb9fe3032036b3415e60c728f692'.

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 

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 035a128 a
((HEAD detached at 035a128))$

Note: You generally only need to use the first few characters of the SHA—as the first four or five characters of the string are most likely unique across the project.

What is a detached HEAD state?

The result of checking out a specific commit puts you in a “ detach HEAD state. ” From the documentation :

[a detached HEAD state] means simply that HEAD refers to a specific commit, as opposed to referring to a named branch

basically, the HEAD ( one of Git ‘s internal pointers that tracks where you are in the Git history ) has diverted from the known branches, and so changes from this target would form a new nerve pathway in the Git history. Git wants to make certain that that is what you are intending, so it gives you a “ detached space ” of sorts to experiment—as described by the output :

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.

From this side you have two options :

  • Experiment and then throw away your changes by returning to your previous branch
  • Work from here and start a new branch from this point

You can use the git switch - command to undo any changes you make and return to your previous branch. If you alternatively want to keep your changes and continue from here, you can use git switch -c to create a fresh branch from this point.


The git checkout control is a utilitarian and multi-purpose control. You can use it to create new branches, checkout a branch, checkout specific commits, and more. If you liked this tutorial, I besides talk about topics like this on Twitter, and write about them on my site .

informant :
Category : Tech

About admin

I am the owner of the website, my purpose is to bring all the most useful information to users.

Check Also


Manage participants in a zoom meeting webinar

Call the people who attend the meet as follows Alternate host host Who scheduled the …

Leave a Reply

Your email address will not be published.