The Two Commands
git branch -d feature-login
git push origin --delete feature-loginThe first removes the branch from your clone. The second removes it from the remote repository named origin. Run only the one you need.
Look Before You Delete
List every branch, local and remote-tracking, and see which one you are on (marked with *):
git status
git branch -aCheck which branches are already merged into main, and which commits a branch has that main does not:
git branch --merged main
git branch --no-merged main
git log --oneline main..feature-loginIf the last command prints nothing, the branch holds no commits that main lacks.
Delete a Local Branch
git branch -d deletes a branch only if its commits are merged into its upstream branch, or into the current branch when it has no upstream. Otherwise Git refuses with a message that the branch is not fully merged. You also cannot delete the branch you are on; switch away first:
git switch main
git branch -d feature-loginGit prints a line such as Deleted branch feature-login (was 1a2b3c4). Note the short hash: it is the quickest way back if you change your mind.
Force Delete with -D
Warning: -D deletes the branch even if its commits exist nowhere else. Run git log --oneline main..BRANCH first and make sure you no longer need what it shows.
git branch -D experimentDelete a Remote Branch
git push origin --delete feature-loginThe older form git push origin :feature-login does the same. If the remote refuses, the branch is probably protected in your hosting service's settings, or your account lacks permission to delete it. Other people's clones keep their copies until they prune.
Remove Stale Remote-Tracking Branches
When a branch is deleted on the remote, your clone still lists origin/feature-login in git branch -a. Remove such leftovers with:
git fetch --pruneTo prune on every fetch, run git config --global fetch.prune true. Pruning touches only remote-tracking names, never your own local branches. For what fetch does in general, see git pull vs git fetch.
Delete All Merged Branches
Review the list first, then pass it to git branch -d, which still refuses anything unmerged:
git for-each-ref --merged=main --format='%(refname:short)' refs/heads/ | grep -vx main
git for-each-ref --merged=main --format='%(refname:short)' refs/heads/ | grep -vx main | xargs -r git branch -dThis prints plain branch names, without the * and + markers that git branch adds. Git refuses to delete the branch you are on and any branch checked out in another worktree; it reports those and deletes the rest.
Recover a Deleted Branch
Deleting a branch removes the name, not the commits. If you have the hash from the delete message, recreate the branch:
git branch feature-login 1a2b3c4Without the hash, search the reflog, which records where HEAD has been in your clone:
git reflog --date=relativeFind the last commit you made or checked out on that branch, then run git branch feature-login HASH.
- Can be recovered: commits that were in your clone, for as long as the reflog keeps them (by default 30 days for entries whose commits are no longer on the current branch, 90 days for the others).
- Cannot be recovered with reflog: commits that were never in your clone, such as someone else's work on a remote branch you never fetched, and commits already removed by garbage collection.
- A deleted remote branch: anyone with a copy can push it back with
git push origin feature-login.
The same reflog rescue works after a bad reset; see git reset explained. To keep unfinished work without a branch, use git stash.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations