Deleting Merged Branches
After you merge a feature branch into main, the branch has served its purpose. The commits it contained are now part of main’s history. The branch pointer itself is no longer needed — it is just a name pointing to a commit that main also points to.
Leaving merged branches around does not break anything, but repositories accumulate them quickly. Deleting branches after merging is the professional habit that keeps a repository legible.
Deleting a merged branch
Section titled “Deleting a merged branch”After merging feature/add-river-canyon into main, delete the branch:
git branch -d feature/add-river-canyonOutput:
Deleted branch feature/add-river-canyon (was c2a1d08).The commit c2a1d08 is not deleted — it still exists in main’s history. Only the branch pointer (the file containing the hash) was removed.
The safety check
Section titled “The safety check”The -d flag has a built-in safety check: it refuses to delete a branch that has commits not yet merged into the current branch. If you try to delete a branch with unmerged work:
git branch -d feature/unfinished-workerror: The branch 'feature/unfinished-work' is not fully merged.If you are sure you want to delete it, run 'git branch -D feature/unfinished-work'.This protects you from accidentally deleting work. If you genuinely want to discard the unmerged commits, use the uppercase -D:
git branch -D feature/unfinished-work-D is a force delete. The commits on that branch become unreachable (they still exist in Git’s object store for a while but are not accessible through normal commands). Use it only when you are certain you want to abandon the work.
Confirming what is merged
Section titled “Confirming what is merged”Before cleaning up, you can list which branches are fully merged into main:
git branch --merged mainAnd which ones are not:
git branch --no-merged mainOnly branches in the --merged output are safe to delete with -d.
Deleting a remote branch
Section titled “Deleting a remote branch”When you push branches to GitHub, the remote has its own copy of each branch pointer. Deleting a local branch does not delete the remote. To delete the remote branch as well:
git push origin --delete feature/add-river-canyonOr the shorter equivalent:
git push origin :feature/add-river-canyonOn GitHub’s web interface, there is also a “Delete branch” button that appears after a pull request is merged — most teams use that rather than the command line for remote cleanup.
Pruning stale remote-tracking references
Section titled “Pruning stale remote-tracking references”When a remote branch is deleted on GitHub, your local repository still has a reference to it (visible in git branch -a as remotes/origin/feature/add-river-canyon). To remove stale references:
git fetch --pruneThis syncs your local list of remote branches with what actually exists on the remote.
Exercise
Section titled “Exercise”- In your
git-practicefolder, check which branches you have:
git branch- Confirm which ones are merged into
main:
git branch --merged main- Delete the merged branches with
-d:
git branch -d feature/add-river-canyongit branch -d feature/tour-descriptions-
Run
git branchagain. Onlymain(and any branches with unmerged work) should remain. -
Try deleting a branch with unmerged work using
-d. Read the error message. Then decide whether to use-Dor switch back to merge it first.
git branch -d <name>deletes a branch that has been fully merged into the current branch. The commits are not deleted — they still exist in history.- The
-dflag refuses to delete branches with unmerged commits. This is a safety feature. git branch -D <name>force-deletes a branch regardless of merge status. Use with intention.git branch --merged mainlists branches safe to delete.git branch --no-merged mainlists branches with unmerged work.git push origin --delete <name>deletes a branch on the remote.git fetch --prunecleans up stale remote-tracking references.
Next: the Module 03 recap — a consolidated reference for every branching concept and command, and a preview of Module 04 on undoing changes.