Menu
Coddy logo textTech

Git: Create a Branch

Last updated

The modern way to create a branch and switch to it in one step is git switch -c <name>. The older git checkout -b <name> does the same thing and still works everywhere. git branch <name> creates the branch without switching to it. A new branch starts from wherever you are now unless you name a different starting point.

Try these in the terminal playground - a real shell in your browser, nothing to install.

Creating a branch starts a new line of commits from where you are - the new pointer is instant and free.

Syntax

CommandWhat it does
git switch -c featureCreate feature and switch to it (modern)
git checkout -b featureCreate and switch (older, equivalent)
git branch featureCreate the branch without switching
git switch -c feature mainCreate feature starting from main
git switch -c feature <hash>Create a branch starting from a commit

Common cases

GoalCommand
Branch off a remote branchgit switch -c feature origin/feature
Push a new branch and set upstreamgit push -u origin feature
Create a branch you'll switch to latergit branch feature

Git create branch FAQ

How do I create a branch and switch to it in one command?
Run git switch -c <name> (modern Git) or the equivalent git checkout -b <name>. Both create the new branch from your current commit and immediately check it out, so you're ready to work on it right away.
What's the difference between git switch -c and git checkout -b?
They do the same thing - create a branch and switch to it. git switch is the newer, more focused command introduced to separate branch-switching from git checkout's many other uses. git checkout -b is older but still fully supported; use whichever your team prefers.
How do I create a branch from another branch or a specific commit?
Add a starting point as the last argument: git switch -c feature main branches off main, and git switch -c feature <hash> branches off a specific commit. Without a starting point, the branch is created from your current HEAD.
How do I push a new branch to the remote?
After creating it locally, run git push -u origin <name>. The -u sets the upstream so future git push and git pull on that branch need no arguments. The branch doesn't exist on the remote until you push it.
Can I practice this online?
Yes. Open the terminal playground to run these commands in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers branching end to end.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED