Git & GitHub
/Beginner
git branch
Definition
The command used to list, create, or delete branches.
Explain Like I'm New
The branch management tool. Typing it by itself lists all your branches and puts a little star next to the one you are currently on. Typing it with a name creates a new alternate universe.
Real World Example
Your boss tells you to build a login page. You type `git branch feature/login-page` to create a safe space to work.
Common Use Cases
- •Seeing where you are
- •Creating new branches
- •Deleting old branches
Crucial Command Flags
| Flag / Option | Description |
|---|---|
| -a | All. List both remote-tracking and local branches. |
| -d | Delete. Delete a local branch (prevents deletion if unmerged). |
| -D | Force Delete. Delete a branch regardless of merge status. |
| -m | Move/Rename. Rename the current branch. |
Terminal Output
bash / terminal
# List all local branches (the one with the * is active)
$ git branch
# feature-login
# * main
# Create a new branch called 'dark-mode'
$ git branch dark-mode
# Delete a branch (safe delete)
$ git branch -d old-feature
# Force delete a branch (even if work will be lost)
$ git branch -D broken-feature
Interview Questions
basic
- Does creating a branch automatically switch you to that branch?
intermediate
- How do you delete a branch safely?