# 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`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.eliasnorrby.com/git/get-current-branch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
