Skip to content

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.

After merging feature/add-river-canyon into main, delete the branch:

Terminal window
git branch -d feature/add-river-canyon

Output:

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 -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:

Terminal window
git branch -d feature/unfinished-work
error: 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:

Terminal window
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.

Before cleaning up, you can list which branches are fully merged into main:

Terminal window
git branch --merged main

And which ones are not:

Terminal window
git branch --no-merged main

Only branches in the --merged output are safe to delete with -d.

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:

Terminal window
git push origin --delete feature/add-river-canyon

Or the shorter equivalent:

Terminal window
git push origin :feature/add-river-canyon

On 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.

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:

Terminal window
git fetch --prune

This syncs your local list of remote branches with what actually exists on the remote.

  1. In your git-practice folder, check which branches you have:
Terminal window
git branch
  1. Confirm which ones are merged into main:
Terminal window
git branch --merged main
  1. Delete the merged branches with -d:
Terminal window
git branch -d feature/add-river-canyon
git branch -d feature/tour-descriptions
  1. Run git branch again. Only main (and any branches with unmerged work) should remain.

  2. Try deleting a branch with unmerged work using -d. Read the error message. Then decide whether to use -D or 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 -d flag 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 main lists branches safe to delete. git branch --no-merged main lists branches with unmerged work.
  • git push origin --delete <name> deletes a branch on the remote.
  • git fetch --prune cleans 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.