HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Create a new Git branch

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
branchcreatenewgit

Problem

Git branches are used to develop features, fix bugs, and experiment with new ideas. You can easily create a new branch using the git checkout command.
Creating a new branch is as simple as using git checkout -b <branch>. This command creates a new branch with the specified name and switches to it. You can also set up a remote tracking branch for the newly created branch by adding -t <remote>/<branch>.
> [!TIP]
>
> You can alternatively use git branch <branch> [-t <remote>/<branch>] and then git checkout <branch> separately.

Solution

# Syntax: git checkout -b <branch> [-t <remote>/<branch>]

git checkout -b patch-1
# Local branch, without a remote tracking branch

git checkout -b patch-2 -t origin/patch-2
# Local branch and remote tracking branch with the same name


> [!TIP]
>
> You can alternatively use git branch <branch> [-t <remote>/<branch>] and then git checkout <branch> separately.
If you want to create an empty branch without any history, you can use git checkout --orphan <branch>. This command creates a new branch with no commit history. This can be very useful for setting up branches with entirely different content or history from your main branch (e.g. gh-pages).

Code Snippets

# Syntax: git checkout -b <branch> [-t <remote>/<branch>]

git checkout -b patch-1
# Local branch, without a remote tracking branch

git checkout -b patch-2 -t origin/patch-2
# Local branch and remote tracking branch with the same name
# Syntax: git checkout --orphan <branch>

git checkout --orphan gh-pages
# Creates a new branch named `gh-pages` with no commit history

Context

From 30-seconds-of-code: create-branch

Revisions (0)

No revisions yet.