# Git: handy branching commands

Here’s a little cheat sheet to help deal with branches.

For the concepts behind these commands, start with
[Git branches, merging, and rebasing](/git-dealing-with-branches-merging-and-rebasing).

## Listing branches

List branches:

```shell
$ git brach
```

List remote branches:

```shell
$ git branch --remote # Or -r for short
```

List remote branches that are not tracked:

```shell
$ git ls-remote --heads <remote-name>
```

## Making and editing branches

Make a new branch based on the active branch:

```shell
$ git branch <branch-name>
```

Rename a branch:

```shell
$ git branch -m <old-name> <new-name>
```

Rename the active branch:

```shell
$ git branch -m <new-name>
```

## Deleting branches

Delete a branch in a safe manner:

```shell
$ git branch -d <branch-name>
```

Git docs on the `-d` flag:

> Delete a branch. The branch must be fully merged in its upstream branch, or in
> HEAD if no upstream was set with

If a branch that is not merged is wanted to be deleted, use the capital D `-D`
flag to force it:

```shell
$ git branch -D <branch-name>
```

Delete remote branch:

```shell
$ git branch -d -r origin/<branch-name>
```

Kernel.org has
[great git docs](https://www.kernel.org/pub/software/scm/git/docs/git-branch.html).
