# Git branches

A Git branch is a movable pointer to a commit, not a copy of your files. That is why creating one costs nothing, and the git branch command is what you use to list, create, rename and delete branches.

## A branch is a pointer

Inside the repository a branch is stored as a single line holding a commit hash.
When you make a new commit, the pointer of the current branch moves to it — and
only that pointer. A separate pointer called `HEAD` remembers which branch you
are on right now.

Everything else follows from this. Creating a branch is instant, no matter how
many files the project has. Ten branches do not take ten times the space. And
deleting a branch does not delete commits: only the name disappears, the commits
stay as long as something still refers to them.

## See what you have

```bash
git branch
```

```
  feature/login
* main
  hotfix-404
```

The asterisk marks the current branch. Useful variations of the listing:

| Command | What it shows |
| --- | --- |
| `git branch -v` | the latest commit of every branch on one line |
| `git branch -vv` | the same plus the tracked branch on the server |
| `git branch -a` | both local and remote branches |
| `git branch -r` | remote branches only (`origin/…`) |
| `git branch --show-current` | just the current name — handy in scripts |
| `git branch --merged` | branches already merged into the current one: candidates for deletion |

## Create, switch, delete

```bash
git branch feature/export
```

The branch exists now, but you are still where you were. Usually you want to
move to it straight away:

```bash
git switch -c feature/export
```

```
Switched to a new branch 'feature/export'
```

The `switch` and `restore` commands appeared in Git 2.23 to split up the job of
the old `checkout`: it could both change branches and restore files, which made
it easy to use in the wrong way. The old form `git checkout -b feature/export`
still works — see [git checkout](https://skillok.org/topics/git-checkout-command) for the details.

Deleting:

```bash
git branch -d feature/export
```

```
Deleted branch feature/export (was 9d31c07).
```

Lowercase `-d` refuses to delete a branch whose work has not been merged
anywhere — `error: the branch 'feature/export' is not fully merged`. That is a
safeguard, not a bug: make sure you really do not need those commits and repeat
with uppercase `-D`. This does not touch the branch on the server; you delete
that one with a push — see [git push](https://skillok.org/topics/git-push-command).

## Detached HEAD

If you switch not to a branch but to a specific commit or tag, Git says:

```
Note: switching to '4f1c2ab'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them...
```

Nothing bad has happened: you are simply looking at an older state of the
project, and `HEAD` points straight at a commit instead of at a branch. There is
only one danger — commits you make in this state do not belong to any branch, and
after you switch away they will have no name.

How you get out depends on what you did. If you made no commits, go back to the
branch you were on:

```bash
git switch -
```

If you made commits and you need them, give them a name right where you are:

```bash
git switch -c experiment-sort
```

If you already left and the commits were left without a name, find their hash in
`git reflog` and create a branch from it: `git branch rescue 3c9f011`.

## Renaming and keeping the list tidy

```bash
git branch -m old-name new-name
```

Without the first name, the current branch gets renamed — this is how people
change `master` to `main` in an old repository. After a rename the branch on the
server keeps its old name, so you have to push the new one and delete the old
one there.

Every couple of weeks it pays to clean up the list: `git branch --merged` shows
merged branches, and `git fetch --prune` removes remote branches from your local
list once they no longer exist on the server. Keep names descriptive —
`feature/export-csv`, `fix/login-500` — and the branch list reads like a to-do
list rather than a jumble of letters. The other everyday commands are collected
in the [Git commands cheat sheet](https://skillok.org/topics/git-commands-cheat-sheet).

## Step-by-step plan

1. **Check where you are** — git status or git branch --show-current before any switch.
2. **Create a branch from the right place** — git switch -c name; give a starting point if needed: git switch -c name main.
3. **Work and commit** — Commits move only this branch's pointer; main stays where it was.
4. **Merge the work** — Go back to main and merge the branch; after that git branch --merged lists it.
5. **Delete the finished name** — git branch -d name locally and git push origin --delete name on the server.

## Check yourself

1. Which command creates a branch called feature and switches to it at once?
   - git switch -c feature
   - git branch feature
   - git switch feature
2. Git said “You are in 'detached HEAD' state” and you have already made commits. What keeps them?
   - git switch -c branch-name
   - git reset --hard HEAD
   - git branch -D branch-name
3. Which command lists the branches already merged into the current one?
   - git branch --merged
   - git branch -r
   - git branch --show-current

## Sources

- [Pro Git book](https://git-scm.com/book/en/v2) — The chapter “Git Branching” with diagrams of the pointers
- [git branch reference](https://git-scm.com/docs/git-branch) — Options for listing, creating and deleting
- [git switch reference](https://git-scm.com/docs/git-switch) — The modern command for switching branches

---

https://skillok.org/articles/en/git-branches
