Git Branch: How to Create, List, Switch, and Delete Branches

A Git branch is a movable pointer to a line of development. It lets you work on a feature, a bug fix, or an experiment in isolation, without touching the stable code on your main branch. When the work is ready, you merge it back. When it isn’t, you throw the branch away and nothing else is affected.

Branching is the single most important habit that separates confident Git users from nervous ones. Once you understand how cheap and reversible branches are, you stop being afraid to experiment. This guide covers everything you need to manage branches day to day: creating, listing, switching, renaming, and deleting them, using both the classic `git checkout` and the modern `git switch`.

Key Takeaways
• A branch is a movable pointer to a commit, not a copy of your files — which is why creating one is instant.
Create a branch with `git branch name`, or create and switch in one step with `git switch -c name` (modern) or `git checkout -b name` (classic).
List branches with `git branch` (local), `git branch -a` (all, including remote), and `git branch -r` (remote only).
Switch branches with `git switch name` (modern, clearer) or `git checkout name` (classic).
Delete a merged branch safely with `git branch -d name`; force-delete with `git branch -D name`; remove a remote branch with `git push origin –delete name`.

What is a Git branch, really?

A branch in Git is just a lightweight, movable label that points at a single commit. The default branch is usually called `main` (older repositories use `master`). When you make a new commit, the branch label automatically moves forward to point at that new commit.

This matters because it changes how you should think about branching. A branch does not duplicate your project files. It does not create a folder. It creates a tiny pointer — a few bytes — that references an existing commit in your history.

You can prove this to yourself. Branch references live as plain files under `.git/refs/heads/`:

“`bash

cat .git/refs/heads/main

“`

That single line is the entire branch. Everything else — your commit history, your working files — is shared.

The mental model that makes Git branches finally click: a branch is NOT a copy of your files — it’s a lightweight, movable LABEL pointing at a commit. This is why branching in Git is instant and cheap: no files are duplicated, Git just creates a new pointer. And it’s why you should branch *freely*. Beginners hesitate to branch because they imagine it’s like copying the whole project to a new folder — expensive, messy, something you do sparingly. But Git branches cost essentially nothing, so the expert habit is the exact opposite: never work directly on `main`, spin up a branch for even the smallest change, and delete it once it’s merged. Once you internalize “a branch is a pointer, not a copy,” the whole workflow makes sense. You create dozens of them without a second thought, switch between them in an instant, and throw them away freely — because each one is just a label, and labels are free.

How do you create a Git branch?

The most basic command creates a new branch but leaves you exactly where you are:

“`bash

git branch feature-login “`

Notice that you are still on your original branch after this. `git branch name` only *creates* the pointer; it does not move you onto it. To start working, you’d still need to switch.

Most of the time you want to create and switch in one step. There are two ways, depending on your Git version:

“`bash

git switch -c feature-login

git checkout -b feature-login “`

Both create `feature-login` and immediately move you onto it. The `-c` in `git switch -c` stands for “create”; the `-b` in `git checkout -b` stands for “branch.” They are functionally equivalent here.

You can also branch from a specific starting point rather than your current commit:

“`bash

git switch -c hotfix-payment main

git switch -c experiment 8f3a2c1d “`

If you’re still finding your footing with init, clone, add, and commit, start with the fundamentals before going deep on branching.

How do you list your Git branches?

To see what branches exist, use `git branch` with no arguments. The current branch is marked with an asterisk:

“`bash git branch

“`

The `*` next to `main` tells you that’s your current branch. This is the fastest way to check where you are.

To see remote branches as well, add `-a` (all):

“`bash git branch -a

“`

Anything prefixed with `remotes/origin/` lives on the remote (the server). To list only remote branches, use `-r`:

“`bash git branch -r

“`

A few useful variations:

“`bash

git branch -v

git branch –merged

git branch –no-merged “`

`git branch –merged` is the safe way to find branches you can delete, because everything it lists has already been integrated.

How do you switch between Git branches?

Switching moves your working directory to match a different branch. As with creation, there’s a modern command and a classic one:

“`bash

git switch feature-login

git checkout feature-login “`

When you switch, Git updates the files in your working directory to match the target branch’s commit. Any committed work on the branch you left is safely stored — switching does not lose it.

A handy shortcut jumps back to the branch you were just on:

“`bash

git switch – “`

If you have uncommitted changes that would conflict with the target branch, Git stops you. You’ll need to commit them, stash them, or discard them first:

“`bash

git stash git switch main

git switch feature-login git stash pop “`

For a deeper look at how `git checkout` handles branches, files, and commit restoration, see the dedicated guide.

Why does `git switch` exist when `git checkout` already does this?

`git checkout` is overloaded. It switches branches, but it *also* restores files, detaches HEAD onto a commit, and more. That double duty caused real confusion and the occasional accidental data loss.

Git 2.23 (released 2019) split the relevant jobs into two clearer commands:

Job Old (overloaded) New (focused)
Switch to a branch `git checkout name` `git switch name`
Create + switch `git checkout -b name` `git switch -c name`
Restore a file `git checkout — file` `git restore file`

`git switch` only deals with branches, so its intent is unambiguous. `git checkout` still works and isn’t going away — but for branch operations, `git switch` is the clearer modern choice.

How do you check which branch you’re currently on?

Two quick ways:

“`bash

git branch

git status

“`

If you just want the name with no decoration, ask Git directly:

“`bash git branch –show-current

“`

That last one is ideal for scripts and prompts because it prints only the branch name.

How do you rename a Git branch?

Renaming uses `git branch -m` (move). Briefly:

“`bash

git branch -m new-name

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

Renaming a branch that’s already been pushed to a remote takes a couple of extra steps, because the old name still exists on the server. The full process — including updating the remote and resetting the upstream — is covered in detail in the rename guide.

How do you delete a Git branch?

Once a branch is merged and you no longer need it, delete it. There are two flavors: safe and forced.

The safe delete uses lowercase `-d`. Git will refuse if the branch has unmerged commits, protecting you from losing work:

“`bash

git branch -d feature-login

“`

If the branch has commits that aren’t merged anywhere, Git stops you:

“`bash git branch -d experiment

“`

The force delete uses uppercase `-D`. It deletes the branch regardless of merge state. Use it deliberately — it can discard commits that exist only on that branch:

“`bash

git branch -D experiment “`

A simple rule: reach for lowercase `-d` by default. Only escalate to `-D` when you genuinely want to abandon the work on a throwaway branch.

How do you delete a remote branch?

Deleting a branch locally does not remove it from the remote. To delete the branch on the server:

“`bash

git push origin –delete feature-login “`

After a teammate deletes remote branches, your local list may still show stale remote-tracking references. Clean them up with:

“`bash

git fetch –prune

git remote prune origin “`

Branches that track a remote counterpart are called tracking branches, and they’re what make `git pull` and `git push` know where to go without extra arguments. For setting up and managing them, see the remote branch guide.

What’s the quick command reference for Git branches?

Here’s the full set in one table:

Task Command
Create a branch `git branch name`
Create and switch (modern) `git switch -c name`
Create and switch (classic) `git checkout -b name`
List local branches `git branch`
List all branches (incl. remote) `git branch -a`
List remote branches only `git branch -r`
Show current branch name `git branch –show-current`
Switch branches (modern) `git switch name`
Switch branches (classic) `git checkout name`
Switch to previous branch `git switch -`
Rename current branch `git branch -m new-name`
Delete merged branch (safe) `git branch -d name`
Force-delete branch `git branch -D name`
Delete remote branch `git push origin –delete name`
Prune stale remote refs `git fetch –prune`

A typical feature workflow strings several of these together:

“`bash

git switch main git pull

git switch -c feature-search

git add . git commit -m “Add search endpoint”

git push -u origin feature-search

git switch main git pull git branch -d feature-search git push origin –delete feature-search “`

Once a branch is merged into another, the next step is usually combining lines of work — that’s where merging comes in.

Run real Git on a server you control with DarazHost

Branching is cheap on your laptop, but deploy workflows happen on a server. DarazHost VPS and dedicated servers give developers full Git on the server over SSH — create, switch, merge, and delete branches and run branch-based deploy workflows in your real environment, with guaranteed resources and 24/7 support.

Instead of fighting shared-hosting limits, you get a genuine Linux environment where `git clone`, `git switch`, post-receive hooks, and branch-per-environment deploys all behave exactly as they do in production. That’s the foundation a serious development setup needs — and it’s covered end to end in our complete guide to hosting for developers, which walks through SSH, Git, and the kind of real environment you actually control.

Frequently asked questions about Git branches

What is the difference between `git switch` and `git checkout`? Both switch branches, but `git checkout` is overloaded — it also restores files and detaches HEAD onto commits. `git switch` (Git 2.23+) was introduced to do one job clearly: move between branches. For branch operations, prefer `git switch`; `git checkout` still works everywhere.

Does creating a branch copy my files? No. A branch is just a movable pointer to a commit — a few bytes referencing existing history. No files are duplicated, which is why creating a branch is instant. This is the key reason you should branch freely for every feature, fix, or experiment.

Why won’t `git branch -d` let me delete my branch? The lowercase `-d` is a safe delete: it refuses if the branch has commits not merged anywhere, to stop you losing work. If you’re sure you want to discard those commits, use the uppercase `git branch -D name` to force the deletion.

How do I delete a branch on the remote, not just locally? Run `git push origin –delete name`. Deleting locally with `git branch -d` only removes your copy; the remote keeps its own branch until you explicitly push the deletion. Afterwards, run `git fetch –prune` to clear stale remote-tracking references.

How do I see which branch I’m currently on? Run `git branch` and look for the asterisk (`*`), check the first line of `git status`, or get just the name with `git branch –show-current`. The last option is best for scripts because it prints the branch name with nothing else.

About the Author

Leave a Reply