A local Git branch is only a name in the current repository, so deleting it cleans the local branch list without removing a branch from a remote repository. Cleanup is safest after the branch has been merged, reviewed, or deliberately abandoned and the working tree is already on the branch that should remain.
git branch -d removes a branch only when Git can prove the branch is fully merged into its upstream branch, or into HEAD when no upstream is configured. That guard keeps local commits from being dropped from normal branch views during routine cleanup.
A checked-out branch cannot be deleted from the same worktree. Switch to main or another branch that should remain first, and use git branch -D only after the branch commits have been pushed, merged, saved elsewhere, or intentionally discarded.
Related: How to create and switch to a Git branch
Related: How to delete a remote Git branch
Related: How to prune stale remote branches in Git
$ git status --short --branch ## main
If the branch to delete is checked out, switch away from it first, for example git switch main.
$ git branch --list cleanup/docs feature/api * main
The asterisk marks the current branch. Do not delete the branch marked with * from the same worktree.
$ git branch --merged main cleanup/docs * main $ git branch --no-merged main feature/api
Delete the target branch from the merged list when doing routine cleanup. Branches listed by git branch –no-merged main still contain commits not reachable from main.
Related: How to merge a Git branch into main
$ git branch -d cleanup/docs Deleted branch cleanup/docs (was 10b8f43).
Use the branch name without refs/heads/. The delete affects the local repository only; it does not remove origin/cleanup/docs or the branch on a remote host.
$ git branch --list feature/api * main
$ git branch -d feature/api error: the branch 'feature/api' is not fully merged hint: If you are sure you want to delete it, run 'git branch -D feature/api' hint: Disable this message with "git config set advice.forceDeleteBranch false"
Do not force-delete a branch just to make the warning go away. Review, merge, push, or archive the commits first when they may be needed later.
$ git branch -D feature/api Deleted branch feature/api (was c7e444a).
$ git branch --list * main $ git status --short --branch ## main