To create a new branch in Git and switch to it, run git switch -c <branch-name>. On Git versions older than 2.23, use git checkout -b <branch-name>, which does exactly the same thing. To create a branch without switching to it, run git branch <branch-name>.

Quick answer:

Create and switch: git switch -c feature/login. Create only: git branch feature/login. Switch: git switch feature/login. List all branches: git branch -a. Put the new branch on GitHub: git push -u origin feature/login.

How we tested: every command on this page was run in a real Git repository (Git 2.43) on 19 November 2026, and the output shown is copied from the terminal. Your commit hashes and branch names will differ.

A branch is a separate line of work inside the same project. You make one when you start a feature or a fix, do all your commits there, and merge it back when it's ready. Your main branch stays working the whole time, and if the idea doesn't pan out you delete the branch and nothing else is touched. This guide covers the Git commands to create a new branch (the right way, with real terminal output), switch between branches, list them, base a branch on an older commit or a remote branch, and push it to GitHub.

What a branch actually is

A branch in Git is a lightweight pointer to a commit, not a copy of your files. That's why creating one is instant, even in a huge project. When you commit on a branch, the pointer moves forward to the new commit, and other branches stay where they were. If you want the bigger picture of commits and repositories first, read what Git is and how it works.

Create a new branch in Git and switch to it

This is the command you'll use most:

$ git switch -c bugfix/header Switched to a new branch 'bugfix/header' 

-c stands for "create". Git makes the branch from the commit you're on right now and moves you onto it in one go. Any new commits land on bugfix/header, and main stays untouched.

The older way: git checkout -b

$ git checkout -b feature/profile Switched to a new branch 'feature/profile' 

Same result. git switch was added in Git 2.23 (2019) because git checkout does too many unrelated jobs, including restoring files, which makes it easy to type the wrong thing. Both commands work today, and you'll see checkout -b in a lot of older tutorials, scripts and Git commands cheat sheets.

Create a new branch without switching to it

$ git branch feature/search $ git branch feature/login feature/search * main 

git branch <name> creates the branch and prints nothing. You stay where you were; the star in the list shows your current branch is still main. This is handy when you want to bookmark the current commit before trying something risky.

Switch between branches

$ git switch feature/search Switched to branch 'feature/search' 

Git swaps the files in your folder to match that branch. Two shortcuts worth knowing:

  • git switch - jumps back to the branch you were on before, like the back button in a browser.
  • git branch --show-current prints just the name of the branch you're on, which helps in scripts.

When Git won't let you switch

Uncommitted edits normally travel with you to the new branch. But if the other branch changed the same file, Git stops to protect your work:

$ git switch feature/search error: Your local changes to the following files would be overwritten by checkout: app.js Please commit your changes or stash them before you switch branches. Aborting 

Nothing is lost. Either commit your changes first, or park them with git stash, switch, and bring them back later with git stash pop. Our git stash guide explains how stashing works.

List branches: local, remote and all

You want to see… Git command
Local branches git branch
Remote branches only git branch -r
All branches, local and remote git branch -a
Branches with their latest commit git branch -v
Which remote branch each one tracks git branch -vv
Only branches matching a pattern git branch --list 'feature/*'

Here's what git branch -a looks like in practice:

$ git branch -a bugfix/header feature/login * feature/profile feature/search main remotes/origin/feature/login remotes/origin/main 

The lines starting with remotes/origin/ are Git's record of the branches on GitHub (or whatever your remote is) as of your last fetch. If a teammate just pushed a branch and you can't see it, run git fetch first.

And git branch -vv tells you which local branches are linked to a remote one:

$ git branch -vv bugfix/header aba805a My change feature/login 796f733 [origin/feature/login] Add login feature/profile aba805a My change feature/search aba805a [origin/feature/search] My change hotfix/typo aba805a My change * main aba805a [origin/main] My change 

Branches with a name in square brackets are tracking a remote branch. bugfix/header has none, which means it only exists on your computer.

Create a branch from another branch or an old commit

By default a new branch starts from wherever you are. Add a starting point at the end to begin somewhere else:

$ git switch -c hotfix/typo aba805a Switched to a new branch 'hotfix/typo' 

The starting point can be a commit hash (find one with git log --oneline), another branch name such as main, or a tag like v1.2.0. A common pattern is starting a fix from the released version: git switch -c hotfix/login v1.2.0.

Create a local branch from a remote branch

When a teammate has pushed a branch you want to work on, fetch it, then switch to it by name:

$ git fetch $ git branch -r origin/HEAD -> origin/main origin/feature/login origin/main $ git switch feature/login Switched to a new branch 'feature/login' branch 'feature/login' set up to track 'origin/feature/login'. 

Git spots that feature/login exists on origin, creates a local branch with the same name and links the two, so git pull and git push work straight away. If you want a different local name, spell it out:

$ git switch -c feature/payments origin/feature/login Switched to a new branch 'feature/payments' branch 'feature/payments' set up to track 'origin/feature/login'. 

Push a new branch to GitHub

A branch you create locally stays on your computer until you push it. The first push needs -u:

$ git push -u origin feature/search To https://github.com/you/my-app.git * [new branch] feature/search -> feature/search branch 'feature/search' set up to track 'origin/feature/search'. 

After that, a plain git push is enough. If you forget -u, Git tells you the branch "has no upstream branch" and prints the exact command to run. Our step-by-step guide to pushing code to GitHub covers login problems and rejected pushes too.

How to name a branch

Git is strict about a few characters. A branch name can't contain spaces, .., ~, ^, :, ?, * or [, can't start with a dash, and can't end with a slash or .lock. Break a rule and you'll see:

$ git branch "bad name" fatal: 'bad name' is not a valid branch name 

Beyond the rules, most teams use a type/short-description pattern, which also groups branches neatly in tools like GitHub:

  • feature/user-signup for new work
  • bugfix/cart-total for fixes
  • hotfix/login-crash for urgent fixes to a release
  • docs/readme-install for documentation

Keep names short, lowercase and hyphenated. Adding a ticket number, like feature/142-user-signup, makes it easy to trace a branch back to its task.

Errors you might see

"fatal: a branch named 'x' already exists"

You already have a branch with that name. Switch to it with git switch x, pick another name, or rename the old one. Our guide shows how to rename a Git branch locally and on GitHub.

"fatal: invalid reference: x"

Git can't find the branch you're switching to. Check the spelling with git branch -a. If it only exists on the remote and doesn't show up, run git fetch and try again. To create it instead of switching to it, add -c.

"You are in 'detached HEAD' state"

You checked out a commit directly instead of a branch. You can look around safely, but new commits won't belong to any branch. Run git switch -c new-branch-name to keep them on a proper branch.

When you're done with the branch

Every time you create a new branch, plan how it will end. Once your work is finished, switch back to main and merge it in with git merge feature/search, or open a pull request on GitHub. If you made several messy commits along the way, you can squash them into one first. Then clean up: git branch -d feature/search deletes the branch locally, and our guide to deleting a Git branch covers removing it from GitHub too. For every other branch command, from git branch --merged to git rebase, see the full list of Git commands.

FAQ: creating branches in Git

What is the Git command to create a new branch?

git switch -c <name> creates a new branch and switches to it. git checkout -b <name> does the same on older Git versions. git branch <name> creates the branch without switching.

What is the difference between git switch -c and git checkout -b?

None in the result: both create a branch and move you onto it. git switch arrived in Git 2.23 as a clearer command that only deals with branches, while git checkout also restores files.

How do I see all branches in Git?

Run git branch -a to list local and remote branches together. Use git branch for local ones only and git branch -r for remote ones only. Run git fetch first so the remote list is current.

How do I switch to another branch?

Run git switch <branch-name>, or git checkout <branch-name> on older Git. Commit or stash your changes first if Git says they would be overwritten.

Does creating a branch copy my files?

No. A Git branch is just a pointer to a commit, so creating one is instant and takes almost no space, no matter how big the project is.

How do I push a new local branch to GitHub?

Run git push -u origin <branch-name>. The -u flag links your local branch to the one on GitHub, so later pushes and pulls need no extra arguments.

Originally published at gitcommands.si, where you'll also find the full list of 233 Git commands with examples.