List branches

bash
git branch              # local branches
git branch -a           # local + remote-tracking
git branch -r           # remote-tracking only
git branch -vv          # local branches with upstream and last commit
git branch --merged     # branches already merged into HEAD
git branch --no-merged  # branches with unmerged work

Create, switch and rename

bash
git switch -c feature/login       # create and switch
git switch main                   # switch
git branch -m old-name new-name    # rename current branch
git branch -m old-name new-name    # rename another branch
git push origin -u new-name       # publish renamed branch

Renaming a published branch requires deleting the old remote branch and pushing the new one. Teammates need to update their local refs.

Delete branches

-d checks that the branch is merged into its upstream or HEAD; -D skips that check. Deleting the remote branch does not delete local copies, and deleting local branches does not remove the reflog entries immediately.

bash
git branch -d merged-branch        # safe: refuses if unmerged
git branch -D unmerged-branch      # force delete
git push origin --delete old-branch # delete on the remote
git fetch --prune                  # remove stale remote-tracking refs

Cleaning up stale refs

When a branch is deleted on the remote, origin/branch may remain locally until you prune. git fetch --prune removes those refs. Some teams set fetch.prune=true globally to keep the list clean automatically.

bash
git config --global fetch.prune true

Recover a deleted branch

A deleted branch's commits remain in the reflog for a while. Find the tip with git reflog, then recreate the branch at that commit.

bash
git reflog
git branch recovered <sha>