Someone asked a human this. So we asked the AI.
How to Delete a Git Branch (Local and Remote)
Branches are cheap, which is why yours have piled up like browser tabs. Getting rid of one takes a single command.
The answer
To delete a local branch, run git branch -d branch-name from a different branch. To delete it on the remote too, run git push origin --delete branch-name.
Delete a local branch
- Switch off the branch you want to delete, for example:
git switch main(older Git:git checkout main). Git will not delete the branch you are currently on. - Run
git branch -d branch-name. - If Git refuses with "not fully merged", the branch has commits that are not merged into your current branch or its upstream. If you are sure you do not need them, force the deletion with
git branch -D branch-name.
-d is the safe version and checks that the work is merged. -D is the same as --delete --force and deletes regardless.
Delete a remote branch
Run:
git push origin --delete branch-nameThis removes the branch from the remote called origin. It does not delete your local copy, so delete that separately with the steps above.
Clean up stale remote-tracking references
If teammates deleted branches on the remote and you still see them in git branch -r, run:
git fetch --pruneCheck what you have
git branchlists local branches.git branch -alists local and remote-tracking branches.
Example
You finished a feature on feature/login and merged it into main.
git switch main
git branch -d feature/loginOutput looks like:
Deleted branch feature/login (was 3f2a9c1).Then remove it from the remote:
git push origin --delete feature/loginOutput includes:
- [deleted] feature/loginWhere people go wrong
Trying to delete the branch you are on gives an error. Switch to another branch such as
mainfirst.Using
-Djust to silence the "not fully merged" error can lose unmerged commits. Check withgit log main..branch-namefirst, or only use-Dwhen you are sure the work is not needed.Deleting the remote branch does not delete the local one, and vice versa. You need both commands to remove it everywhere.
Typing the wrong branch name with
git push origin --deleteaffects the shared remote. Check the name withgit branch -rbefore running it.
The prompt you could have used
I use Git on the command line. Show me how to delete a local branch and the same branch on the remote, including what to do if Git says it is not fully merged.
Updated 29 October 2026. Written by an AI and fact-checked by a second one before it went up; anything they weren't sure of was left out.
Someone asked you something like this? Send them a link →