git checkout -b new-branch creates a branch called new-branch at the commit you are on and switches to it in one step. To start the branch somewhere else, add a start point: git checkout -b new-branch origin/main.
git checkout -b feature/login # new branch at current commit, then switch
git checkout -b hotfix v2.1.0 # start from a tag
git checkout -b feature/login origin/main # start from a remote-tracking branch
git switch -c feature/login # same as the first line, newer command
The command text below follows the official Git manual. Any output shown is example output, not captured from a specific Git version.
What git checkout -b does
The git-checkout manual gives the form git checkout -b <new-branch> [<start-point>] and describes it like this: "Create a new branch named <new-branch>, start it at <start-point> (defaults to the current commit), and check out the new branch."
That is two operations. The git-branch manual says git branch <name> "will create the new branch, but it will not switch the working tree to it". Adding checkout to the same step is the whole point of -b:
git branch feature/login # create only; you stay where you are
git checkout feature/login # switch to it
These two lines give the same result as git checkout -b feature/login. Confirm which branch you are on with git branch --show-current, or look for the asterisk in git branch.
Start the branch from a specific commit, tag or branch
Without a start point, the new branch begins at HEAD, the commit you currently have checked out. The git-branch manual says a start point "may be given as a branch name, a commit-id, or a tag". Examples:
git checkout -b bugfix-123 main # from the local branch main
git checkout -b bugfix-123 9fceb02 # from a commit id
git checkout -b release-fix v2.1.0 # from a tag
This matters because a common mistake is creating a feature branch while sitting on another feature branch. Your new branch then carries that branch's unmerged commits. If you want a clean start from the main line, name it: git checkout -b new-feature main. If your local main is behind, run git fetch first and use origin/main as the start point. The difference between fetching and pulling is covered in git fetch vs git pull.
Branch from a remote branch and track it
To create a local branch from a branch on the remote and set up its "upstream" configuration, use --track:
git checkout -b hack --track origin/hack
The manual describes --track as: "When creating a new branch, set up 'upstream' configuration." The manual also says that if you leave out -b, Git derives the local name from the remote-tracking branch, so git checkout --track origin/hack creates a local hack. If the name has no slash, or the guess comes out empty, the guessing is aborted and you must give a name with -b.
The git-switch manual adds a related shortcut. When you run git switch hack and no local hack exists, but exactly one remote has a branch of that name, Git treats it as git switch -c hack --track <remote>/hack. This is called --guess and is the default. If the branch exists on several remotes, setting checkout.defaultRemote (for example to origin) picks the one to use.
git checkout -b vs git switch -c
git switch -c is the same operation under a newer command. The git-switch manual says -c (--create) will "create a new branch named <new-branch> starting at <start-point> before switching to the branch".
| Goal | git checkout | git switch |
|---|---|---|
| Create and switch | git checkout -b name | git switch -c name |
| Create from a start point | git checkout -b name start | git switch -c name start |
| Create, or reset if it exists | git checkout -B name | git switch -C name |
| Create from remote and track | git checkout --track origin/name | git switch --track origin/name |
The git-switch manual also notes that -c is "transactional": the branch is not created or reset unless the switch succeeds. For everyday use you will not see a difference. Which one to use is a style choice, since git checkout -b is still documented and works. The same -b habit also shows up when rescuing work from a detached HEAD, which this article walks through.
What happens to uncommitted changes
The new branch starts at the commit you are already on, so there is nothing to overwrite and your uncommitted edits stay in the working tree. This is the standard way to move accidental work off main: run git checkout -b my-feature, then commit as normal.
The manual says creation "will fail if there's an error checking out <new-branch>, for example if checking out the <start-point> commit would overwrite your uncommitted changes." That can happen when you give a start point whose files differ from your edited files. The manual's example message for a refused switch is:
error: You have local changes to 'frotz'; not switching branches.
Commit the changes, or put them aside with git stash (see git stash explained), then run the command again.
What if the branch already exists
With -b Git refuses to create a branch whose name is taken. Use a different name, or switch to the existing one with git checkout name.
The -B option is different. The manual says it is "the same as -b, except that if the branch already exists it resets <branch> to the start point instead of failing." That moves the existing branch pointer. Before using -B, look at what the branch holds with git log name --oneline -5. Commits that nothing else points to become hard to find, though git reflog can usually recover them for a while.
Valid branch names
The new name must pass the rules in git-check-ref-format. The ones that trip people up, from its manual:
- No spaces, and none of
~^:?*[or\. - No two consecutive dots (
..) and no@{sequence. - It cannot be the single character
@, end with a dot, or begin or end with a slash. - A slash-separated part cannot begin with a dot or end with
.lock. - For branch names specifically, it cannot begin with a dash.
Slashes are fine for grouping, such as feature/login or fix/typo. To test a name without creating anything, run git check-ref-format --branch "my branch"; it prints an error and exits non-zero for an invalid name.
Push the new branch
git checkout -b only creates a local branch. The remote knows nothing about it until you push:
git push -u origin feature/login
The -u sets the upstream so later git pull and git push work without arguments. If you pick a poor name and want to change it later, see how to rename a Git branch, and to remove a finished one, how to delete a Git branch.
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| New branch contains commits you did not expect | You were on another feature branch, so HEAD was the start point | Recreate it with an explicit start point, such as git checkout -b name main |
| Branch name rejected | It breaks a git-check-ref-format rule (space, .., leading dash and so on) | Test with git check-ref-format --branch "name" and rename |
error: You have local changes to '...'; not switching branches. | The start point's files differ from your edited files | Commit or stash first |
| Branch is missing on GitHub | Creating a branch is local only | git push -u origin name |
| Existing branch moved unexpectedly | You used -B instead of -b | Find the old commit in git reflog and recreate the branch there |
Frequently asked questions
- What does git checkout -b do?
- It creates a new branch with the name you give, starting at the current commit unless you name another start point, and then checks that branch out. It is the one-step version of running git branch new-name followed by git checkout new-name.
- What is the difference between git checkout -b and git checkout -B?
- With -b, Git fails if a branch with that name already exists. With -B, per the Git manual, if the branch already exists it is reset to the start point instead of failing. Because -B moves an existing branch, commits that were only reachable from the old position can become hard to find.
- Is git checkout -b deprecated?
- The Git manual does not mark it as deprecated, and it still works. git switch -c does the same job and was added to split the branch-related half of git checkout into a clearer command. Use whichever you prefer; the two accept the same new-branch and start-point arguments.
- Do my uncommitted changes come with me to the new branch?
- Yes, in the common case. The new branch starts at the same commit, so nothing needs to change in your files and your uncommitted edits stay in the working tree. The command fails only if checking out the start point would overwrite uncommitted changes.
- Does git checkout -b push the branch to the remote?
- No. It only creates a local branch. To publish it, push it yourself, for example with git push -u origin new-name.