Video in progress

Strong Junior 🌿 Git

How do you delete a finished branch locally and on the server? What is the difference between `git branch -d` and `-D`?

-d deletes the branch only if it is fully merged, and otherwise warns you. -D deletes it without asking anything. The branch on the server is deleted with git push origin --delete <name>.

📖 Answer

git branch -d feature/export     # if not merged: a "not fully merged" error
git branch -D feature/experiment # the experiment is not needed: force delete
git push origin --delete feature/export

The warning from -d is a useful safeguard. But in one case it raises a false alarm: if the PR was squash-merged on GitHub, main has one new commit instead of your commits. Git considers the branch "not merged", even though the code is in main. In that case you use -D.

Useful for keeping things tidy locally:

git fetch --prune     # removes the origin/... copies of branches deleted on the server
git branch --merged main   # the list of local branches merged into main

Many teams turn on "Automatically delete head branches" on GitHub, so the server side cleans itself up.