Skip to content
LatchkeyLatchkey home

git branch: Usage, Options & Common CI Errors

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

FlagWhat it does
-d / --deleteDelete a merged branch
-DForce-delete an unmerged branch
-m / --moveRename a branch
-a / --allList 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@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git 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.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card