Git Cheat Sheet · Branches
Delete a branch
git branch -D <branch> · git push origin --delete <branch>
A branch is deleted separately locally and on the remote. git branch -d <branch> deletes the local branch if it has been merged, -D forces the deletion. git push origin --delete <branch> deletes it on GitHub or GitLab.
What it does
Removes a local or remote branch; commits already merged elsewhere stay in the history.
A use case
Your pull request is merged, the working branch is no longer needed.
Warning: Commits that exist only on this branch are no longer reachable from the interface.
On the command line
git branch -d <branch>- Deletes a local branch already merged.
git branch -D <branch>- Deletes it even if it is not merged.
git push origin --delete <branch>- Deletes the branch on the remote.
git fetch --prune- Removes references to branches deleted on the remote.
git branch --merged main- Lists branches already merged into main, which you can safely delete.
In Merathon
- Local: right-click the branch → "Delete". Ctrl + click selects several to delete at once.
- Remote: right-click the branch in "Remote" → "Delete the remote branch".
Frequently asked questions
How do I recover a branch deleted by mistake?
Find its last commit with git reflog, then git branch <branch> <hash>.
Why does Git refuse to delete my branch?
With -d, Git refuses an unmerged branch to avoid losing commits. It also refuses to delete the current branch: switch to another branch first.