Git Cheat Sheet · Branches

Create a branch

git switch -c <branch>

A branch is an independent line of development. git switch -c <name> creates a branch from the current commit and switches to it: your next commits will not affect main.

What it does

Creates a new line of development from a commit, without touching other branches.

A use case

You are starting a feature and want to isolate it from main until it is ready.

Warning: A branch cannot "contain" another: toto/a5 is refused as long as toto exists, and vice versa. Choose toto-a5, for example.

On the command line

git switch -c <branch>
Creates the branch and switches to it.
git checkout -b <branch>
Older syntax, equivalent.
git branch <branch>
Creates the branch without switching to it.
git switch -c <branch> <commit>
Starts the branch from another commit, another branch or a tag.
git push -u origin <branch>
Publishes the branch and links it to its remote version.

In Merathon

  1. "Branch" button in the toolbar: the branch starts from the current commit.
  2. To start from elsewhere: right-click a commit in the graph → "Create a branch here…", or right-click a branch → "Create a branch from…".
  3. The "Switch to the new branch" checkbox makes it current right away.

Frequently asked questions

How should I name a branch?

A prefix that gives the type of work, then a few words: feature/pdf-export, fix/mobile-menu. No spaces or accents.

Do my uncommitted changes follow the new branch?

Yes: creating a branch does not touch the working copy. They will be committed on the new branch.

Why is the name `foo/a5` refused?

Git stores branches like files: foo cannot be both a file and a folder. As long as the foo branch exists, choose foo-a5, for example. The reverse is also true.

See also