> For the complete documentation index, see [llms.txt](https://notes.eliasnorrby.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.eliasnorrby.com/git/get-current-branch.md).

# Name of current branch

To get the name of the current branch, we can use `git rev-parse`:

```bash
git rev-parse --abbrev-ref HEAD
```

This will print the name of the current branch *as long as we have one checked out*. If we're in a detached HEAD state, it will print HEAD.

Since version 2.22, `git branch` has a `--show-current` option. It will give us the name of the current branch, but intead will print nothing if we're in a detached HEAD state.

```bash
git branch --show-current
```

Source: `man git-rev-parse`, `man git-branch`
