Delete a local branch with git branch -d
To delete a local branch in Git, use the command git branch -d branch-name, replacing "branch-name" with the actual name of the branch you want to remove. This command deletes the branch from your computer only — it does not affect branches on a remote server like GitHub or GitLab.
Before you delete, make sure you are not currently on the branch you want to delete. Switch to a different branch first (usually main or master) by running git checkout main. If you try to delete the branch you are on, Git will refuse and show an error message.
The -d flag is the safe option. Git will reject the deletion if the branch contains work that has not been merged into another branch yet. This prevents you from accidentally losing commits.
Key Takeaways
- Use git branch -d branch-name to delete a local branch safely — Git will refuse if the branch has unmerged work.
- Switch away from the branch first with git checkout main, because you cannot delete the branch you are currently on.
- If Git refuses the deletion, either merge the branch first or use -D (uppercase) to force deletion — but only if you are certain the work is saved elsewhere.
- Deleting a local branch does not remove it from GitHub, GitLab, or other remote servers; you must delete the remote branch separately if needed.
- Use git branch -a to see all branches (local and remote) before deciding which ones to delete.
Check which branch you are on before deleting
Git will stop you from deleting a branch while you are on it. To see which branch you are currently working on, run git status. The output will show your current branch name at the top, usually in a line that says "On branch main" or "On branch feature-login".
If you are on the branch you want to delete, switch to a different branch first. The safest choice is usually main or master (whichever is your primary branch). Run git checkout main to switch. Once you are on a different branch, you can delete the one you just left.
Force delete with -D if the branch has unmerged work
If you run git branch -d branch-name and Git refuses with a message like "error: The branch 'branch-name' is not fully merged", it means the branch contains commits that are not in any other branch. Git is protecting you from losing work.
You have two choices. First, you can merge the branch into another branch before deleting it — run git checkout main, then git merge branch-name, then delete. Second, if you are certain the work is saved elsewhere or you do not need it, you can force the deletion with git branch -D branch-name (uppercase D). The uppercase flag skips the safety check and deletes immediately. Use this only when you are sure.
See all your branches before deleting
To see a list of all local branches on your computer, run git branch. The current branch will have an asterisk (*) next to it. This helps you confirm which branch you want to delete and which one you are on.
If you want to see both local and remote branches together, run git branch -a. Remote branches appear with names like origin/branch-name. This is useful if you are trying to remember whether a branch exists on GitHub or only on your computer.
Delete a remote branch separately if needed
Deleting a local branch does not remove it from GitHub, GitLab, Bitbucket, or any other remote server. If you want to delete the branch there too, you need a separate command: git push origin --delete branch-name. Replace "origin" with the name of your remote if it is different (though "origin" is the standard name).
Many teams delete the remote branch after merging a pull request, then delete the local branch later when cleaning up. You can do either order — delete local first, then remote, or vice versa. They are independent.
Clean up multiple old branches at once
If you have many branches to delete, you can delete them one at a time with separate commands, or you can list them all in one command. For example: git branch -d branch-one branch-two branch-three will delete all three in one go, as long as they are all safe to delete (no unmerged work).
Another useful command is git branch --merged, which shows only branches that have already been merged into your current branch. These are usually safe to delete. You can review the list, then delete them individually or in a batch.
Frequently Asked Questions
What happens if I delete a branch by mistake?
Git keeps a record of deleted branches for about 30 days in the reflog. Run git reflog to see the history, find the commit hash of the branch you deleted, then run git checkout -b branch-name commit-hash to recreate it. After 30 days, the reflog entry expires and recovery becomes much harder.
Can I delete a branch that has been pushed to GitHub?
Yes. Deleting the local branch on your computer does not affect the remote branch on GitHub. To remove it from GitHub too, run git push origin --delete branch-name. You can do this in either order — delete local first or remote first.
Why does Git refuse to delete my branch?
Git refuses when the branch contains commits that are not merged into any other branch. This is a safety feature to prevent losing work. Either merge the branch first, or use git branch -D (uppercase) to force deletion if you are certain the work is saved elsewhere or not needed.
Is there a way to delete all old branches at once?
You can list branches that are already merged with git branch --merged, review the list, then delete them in one command like git branch -d branch-one branch-two branch-three. For remote branches, git push origin --delete branch-one branch-two works the same way.