git branch manages your branches: list them, make them, rename them, delete them.
Use git branch for branch bookkeeping; use switch/checkout to actually move onto a branch.
What it does
git branch with no arguments lists local branches. With a name it creates a branch; with flags it deletes, renames, or lists branches by various filters.
Common usage
Terminal
git branch # list local branches
git branch -a # include remote-tracking branches
git branch feature # create (does not switch)
git branch -d feature # delete if merged
git branch -m old new # rename
Options
Flag
What it does
-d / --delete
Delete a merged branch
-D
Force-delete an unmerged branch
-m / --move
Rename a branch
-a / --all
List local and remote-tracking branches
--set-upstream-to=<ref>
Set the tracked upstream
Common errors in CI
error: The branch 'X' is not fully merged. If you are sure you want to delete it, run 'git branch -D X' - git -d protects unmerged work. Use -D to force. Listing only shows what was cloned, so -a may miss branches if the clone was single-branch.
Using this in CI
CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.
.github/workflows/ci.yml
- uses:actions/checkout@v4with:fetch-depth:0 # history, tags, and git describe all need this- run:|git rev-parse --is-shallow-repository # expect falsegit rev-parse --abbrev-ref HEAD # prints HEAD when detached
Frequently asked questions
git branch: Usage, Options & Common CI Errors?
Use git branch for branch bookkeeping; use switch/checkout to actually move onto a branch.
What it does?
git branch with no arguments lists local branches. With a name it creates a branch; with flags it deletes, renames, or lists branches by various filters.
Common errors in CI?
error: The branch 'X' is not fully merged. If you are sure you want to delete it, run 'git branch -D X' - git -d protects unmerged work. Use -D to force. Listing only shows what was cloned, so -a may miss branches if the clone was single-branch.