git branch: Create, List, Rename, and Delete Branches
`git branch` is the primary command for managing branches themselves — creating new ones, listing what exists, renaming them, and cleaning up ones you no longer need. Note that `git branch` alone doesn't switch you onto a new branch; it only manages branch pointers, while switching your actual working position is handled by `git switch` or `git checkout`, covered in the next lesson.
Learning Objectives
- Create a new branch using git branch <name>.
- List local branches, and all branches including remote-tracking ones.
- Rename a branch using git branch -m.
- Delete a branch safely (-d) and forcibly (-D), understanding the difference.
Key Terms to Know Before Learning git branch
- git branch <name>: Creates a new branch pointer at the current commit (HEAD), without switching to it.
- git branch (no arguments): Lists all local branches, marking the current one with an asterisk.
- git branch -a: Lists both local branches and remote-tracking branches.
- git branch -m <old> <new>: Renames a branch.
- git branch -d / -D: Deletes a branch; -d is a safe delete that refuses if the branch has unmerged changes, while -D forces deletion regardless.
How git branch Actually Works
To create a new branch, simply supply a name:
```
git branch feature/login
```
This creates a new pointer named `feature/login` at your current commit (HEAD) — but importantly, **it does not switch you onto that branch**. You remain on whatever branch you were on before; you'd need `git switch feature/login` (next lesson) to actually move onto it.
To see what branches exist, run `git branch` with no arguments:
```
git branch
feature/login
* main
```
The asterisk marks your currently checked-out branch. This only shows **local** branches by default. To also see remote-tracking branches (references to branches that exist on a remote like `origin`), use `-a` (all):
```
git branch -a
```
Renaming a branch uses `-m` (move):
```
git branch -m old-name new-name
```
Or, if you want to rename your *current* branch, you can omit the old name:
```
git branch -m new-name
```
Deleting a branch has two distinct forms, and the difference matters significantly:
- `git branch -d <name>` performs a **safe delete**. Git checks whether this branch's commits have already been merged into your current branch (or its upstream). If they haven't, Git refuses to delete it and shows an error, protecting you from losing work that only exists on that branch.
- `git branch -D <name>` (capital D, actually shorthand for `--delete --force`) performs a **forced delete**, removing the branch regardless of whether its work has been merged anywhere else. This can permanently lose commits if they aren't reachable from any other branch, so it should be used deliberately, only when you're certain the branch's work is no longer needed (or has genuinely already been captured elsewhere).
A useful safety habit: always try `-d` first, and only reach for `-D` if you've deliberately confirmed you want to discard that branch's unique work — Git's refusal with `-d` is a meaningful warning, not just an obstacle to work around reflexively.
Finally, note that all of these `git branch` operations affect only your **local** repository. Deleting a local branch does not delete a matching branch on a remote like GitHub — that requires a separate remote-branch deletion command, covered in the next lesson on cleanup.
git branch: Visual Walkthrough
Show a simple flowchart: 'git branch feature/x' → creates a new pointer at current commit, but HEAD stays on the old branch (shown with a dotted line to the new branch and a solid line still pointing at 'main'). Below, show a decision diamond for deletion: 'Has this branch's work been merged elsewhere?' → Yes path: 'git branch -d name (safe delete succeeds)'. No path: 'git branch -d name (refuses!) → must use git branch -D name to force delete (risk of losing work).'
Command vs Effect: Key Differences
| Command | Effect |
|---|---|
| git branch <name> | Creates a new branch at the current commit; does NOT switch to it |
| git branch | Lists local branches, marking the current one with * |
| git branch -a | Lists local AND remote-tracking branches |
| git branch -m <old> <new> | Renames a branch |
| git branch -d <name> | Safely deletes a branch (refuses if unmerged work exists) |
| git branch -D <name> | Force-deletes a branch, even if it has unmerged work |
git branch: Command Syntax and Examples
# Create a new branch (does not switch to it)
git branch feature/login
# List local branches
git branch
# * main
# feature/login
# List all branches, including remote-tracking ones
git branch -a
# Rename a branch
git branch -m feature/login feature/user-login
# Safely delete a merged branch
git branch -d feature/user-login
# Force-delete a branch even if unmerged (use with caution)
git branch -D experimental/risky-idea
Breaking Down the git branch Example
`git branch feature/login` creates the new pointer, and the subsequent `git branch` listing confirms `main` (marked with `*`) remains the current branch — creating a branch doesn't move you onto it. `git branch -a` would additionally reveal remote-tracking references like `remotes/origin/main`. The rename example shows updating a branch's name after realizing a clearer name was needed. `git branch -d feature/user-login` succeeds only because its work has already been merged elsewhere; if it hadn't been, this command would fail with a warning, and only the forced `-D` variant — shown in the final line for a genuinely abandoned experimental branch — would remove it regardless.
How git branch Is Used on Real Engineering Teams
- Feature branch naming conventions like feature/, bugfix/, and hotfix/ prefixes (e.g., feature/login, hotfix/payment-crash) are extremely common in team workflows, making git branch -a listings easy to scan and understand at a glance.
- Automated cleanup scripts in CI/CD pipelines often run git branch -d for merged feature branches automatically after a pull request is merged, keeping local and remote branch lists tidy.
- Engineers commonly rely on git branch -d's safety check as a deliberate guardrail — if it refuses to delete a branch, that's treated as a signal to double check whether important work would be lost, rather than immediately reaching for -D.
- Long-lived branch naming, like release/2026-Q3 or support/v1.x, is common in projects that maintain multiple parallel supported versions simultaneously.
git branch Interview Questions and Answers
Q1. Does git branch <name> switch you onto the newly created branch?
No. git branch <name> only creates a new branch pointer at your current commit — it does not change your currently checked-out branch. You need a separate command, such as git switch <name>, to actually move onto that new branch.
Q2. What is the difference between git branch -d and git branch -D?
git branch -d performs a safe delete, refusing to remove a branch if its commits haven't been merged elsewhere, protecting against accidental data loss. git branch -D forces the deletion regardless of merge status, which can permanently discard unmerged work if used carelessly.
Q3. How would you see all branches, including ones that exist only on a remote repository?
Run git branch -a, which lists both local branches and remote-tracking branches (references to branches that exist on remotes like origin), as opposed to plain git branch, which shows only local branches.
git branch Quiz: Test Your Understanding
1. What happens when you run git branch feature/x?
- You are immediately switched onto the new branch feature/x
- A new branch pointer is created at the current commit, but you remain on your current branch
- The current branch is renamed to feature/x
- It deletes any existing branch named feature/x
Answer: B. A new branch pointer is created at the current commit, but you remain on your current branch
Explanation: git branch only creates the new pointer; it does not change which branch is currently checked out. A separate switch/checkout command is needed to move onto it.
2. What is the key difference between git branch -d and git branch -D?
- There is no difference; they are aliases
- -d is a safe delete that refuses if there's unmerged work; -D forces deletion regardless
- -d deletes local branches; -D deletes remote branches
- -d only works on the current branch; -D works on any branch
Answer: B. -d is a safe delete that refuses if there's unmerged work; -D forces deletion regardless
Explanation: -d checks whether the branch's commits have been merged elsewhere and refuses to delete if not, while -D (force delete) removes the branch unconditionally, risking loss of unmerged work.
3. Which command lists both local and remote-tracking branches?
- git branch
- git branch -a
- git branch -m
- git branch -D
Answer: B. git branch -a
Explanation: The -a flag expands git branch's output to include remote-tracking branches in addition to local ones, which the plain command omits.
git branch: Common Mistakes Beginners Make
- Assuming git branch <name> switches you onto the new branch immediately, then being confused why changes are still happening on the old branch.
- Reaching for git branch -D as a default habit instead of trying -d first and respecting its safety warning.
- Forgetting that deleting a local branch does not remove any corresponding branch on a remote repository like GitHub.
- Trying to rename the branch you're currently on without realizing git branch -m new-name (omitting the old name) is the shorthand for that specific case.
git branch: Exam-Ready Quick Notes
- git branch <name>: creates a branch at current commit; does not switch to it.
- git branch / git branch -a: lists local / local+remote-tracking branches, current branch marked with *.
- git branch -m <old> <new>: renames a branch.
- git branch -d (safe, refuses if unmerged) vs git branch -D (force, always deletes).
git branch: Key Takeaways
- git branch manages branch pointers themselves — creation, listing, renaming, deletion — separately from actually switching your working position.
- The safe delete (-d) versus force delete (-D) distinction is an important safeguard against accidentally losing unmerged work.
- Deleting a local branch has no effect on any matching branch that may exist on a remote repository.
Frequently Asked Questions About git branch
Q1. Does creating a branch with git branch switch me to it?
No. git branch <name> only creates the new branch pointer at your current commit — you remain on whatever branch you were previously on. You need a separate command, like git switch <name>, to actually move onto the new branch.
Q2. How do I see a list of all my branches?
Run git branch to see local branches, with your current one marked by an asterisk. Add the -a flag (git branch -a) to also see remote-tracking branches referencing branches that exist on a remote repository.
Q3. What's the difference between -d and -D when deleting a branch?
-d is a safe delete that refuses to remove a branch if its work hasn't been merged elsewhere, protecting you from losing commits. -D forces the deletion regardless, which can permanently discard unmerged work if you're not careful.
Q4. How do I rename a branch?
Use git branch -m <old-name> <new-name>. If you want to rename the branch you're currently on, you can omit the old name: git branch -m <new-name>.
Q5. If I delete a local branch, does it also get deleted from GitHub?
No. git branch -d or -D only affects your local repository. Deleting a corresponding branch on a remote repository like GitHub requires a separate remote-branch deletion command.
Summary
`git branch` is the command for managing branch pointers: `git branch <name>` creates a new branch at the current commit without switching to it, plain `git branch` lists local branches (marking the current one with an asterisk), and `git branch -a` additionally shows remote-tracking branches. `git branch -m <old> <new>` renames a branch. Deletion comes in two forms: `git branch -d <name>` safely refuses to delete a branch with unmerged work, while `git branch -D <name>` forces deletion regardless, risking permanent loss of any commits unique to that branch. All of these operations affect only the local repository, not any corresponding branches on a remote.