Games  

How to Create New Branch in Git Stack Overflow

Switching branches is something you’ll need to do ofttimes in Git.

To do this, y’all can employ the
git checkout
control.

How to create a new co-operative in Git

To create a new co-operative in Git, you lot utilise the
git checkout
command and laissez passer the
-b
flag with a name.

This will create a new branch off of the electric current co-operative. The new branch’due south history will start at the current identify of the branch you “branched off of.”

Assuming y’all are currently on a branch called
master:

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

Hither you can see a new branch created called
my-characteristic
which was
branched off of
master.

How to switch to an existing branch in Git

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

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

There is also a handy shortcut for returning to the previous branch you were on past passing
-
to
git checkout
instead of a branch name:

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

How to checkout a specific commit

To checkout or switch to a specific commit, you tin likewise use
git checkout
and pass the SHA of the commit instead of a branch proper noun.

After all, branches are actually just pointers and trackers of specific commits in the Git history.

How to find a commit SHA

One way to discover the SHA of a commit is to view the Git log.

Popular:   How to Apply for Facebook Level Up Program

You tin view the log by using the
git log
command:

              (my-feature)$ git log commit 94ab1fe28727b7f8b683a0084e00a9ec808d6d39 (Caput -> my-feature, master) Author: John Mosesman <johnmosesman@gmail.com> Engagement:   Monday Apr 12 10:31:11 2021 -0500      This is the second commmit bulletin.  commit 035a128d2e66eb9fe3032036b3415e60c728f692 (blah) Author: John Mosesman <johnmosesman@gmail.com> Date:   Mon April 12 10:31:05 2021 -0500      This is the first commmit bulletin.
              
            

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

This is called the SHA.
A SHA is a unique identifier that is generated for each commit.

To checkout a specific commit, you just need to pass the commit’s SHA as the parameter to
git checkout:

              (my-characteristic)$ git checkout 035a128d2e66eb9fe3032036b3415e60c728f692 Annotation: switching to '035a128d2e66eb9fe3032036b3415e60c728f692'.  Y'all are in 'discrete HEAD' state. You can look around, brand experimental changes and commit them, and you can discard whatsoever commits you brand in this land without impacting any branches past switching back to a branch.  If you lot want to create a new branch to retain commits you create, y'all may do and then (now or later) by using -c with the switch control. Case:    git switch -c <new-branch-name>  Or disengage this operation with:    git switch -  Turn off this communication by setting config variable advice.detachedHead to imitation  Caput is now at 035a128 a ((Caput detached at 035a128))$
              
            

Note: You generally just need to use the first few characters of the SHA—equally the outset four or five characters of the cord are most probable unique across the projection.

What is a detached HEAD land?

The effect of checking out a specific commit puts yous in a “detached HEAD country.”

Popular:   Castle Clash Heroes Ranking 2022

From the documentation:

[a discrete Caput country] ways just that
HEAD
refers to a specific commit, as opposed to referring to a named branch

Basically, the
Caput
(i of Git’southward internal pointers that tracks where you are in the Git history) has diverted from the known branches, and then changes from this signal would form a new pathway in the Git history.

Git wants to brand certain that that is what yous are intending, and so information technology gives you a “free space” of sorts to experiment—equally described by the output:

              You are in 'detached Head' country. You can look effectually, make experimental changes and commit them, and yous tin can discard any commits y'all brand in this country without impacting whatever branches by switching back to a branch.
            

From this position yous have ii options:

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

You can use the
git switch -
command to undo any changes you make and return to your previous co-operative.

If yous instead want to continue your changes and continue from here, you can employ
git switch -c <new-branch-name>
to
create a new branch
from this signal.

Determination

The
git checkout
control is a useful and multi-purpose command.

You tin can use it to create new branches, checkout a branch, checkout specific commits, and more.

If you liked this tutorial, I likewise talk well-nigh topics similar this on Twitter, and write near them on my site.



Acquire to code for costless. freeCodeCamp’south open source curriculum has helped more than than 40,000 people become jobs as developers. Get started

Popular:   Hidden Bad Cactus Horizon 5

Source: https://www.freecodecamp.org/news/git-switch-branch/