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
Steps to delete a local Git branch:
- Open a terminal in the repository and confirm the current branch is not the branch being deleted.
$ git status --short --branch ## main
If the branch to delete is checked out, switch away from it first, for example git switch main.
- List local branches and identify the branch name to remove.
$ 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.
- Check whether the target branch is already merged into the branch that should keep the work.
$ 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
- Delete the merged local branch with git branch -d.
$ 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.
- Confirm the deleted branch no longer appears locally.
$ git branch --list feature/api * main
- Stop when Git refuses to delete an unmerged branch unless the branch commits are no longer needed.
$ 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.
- Force-delete only a deliberately discarded local branch.
$ git branch -D feature/api Deleted branch feature/api (was c7e444a).
- Verify the final local branch list and current branch.
$ git branch --list * main $ git status --short --branch ## main
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.