Course contentsShow
← Data Science
Lesson 2012 of 2,14546. Version Control with GitFree lesson

Deleting Branches

Using git branch -d for merged branches and -D for force deletion of unmerged work.

Deleting Branches

What you'll learn: How to safely clean up branches after merging, and force-delete branches when necessary.

Once you've merged a feature branch into your main branch, that branch has served its purpose. Keeping old branches around clutters your repository and makes it harder to see what work is active. Git provides safe ways to remove branches you no longer need.

Safe Deletion with -d

After a successful merge, use git branch -d to delete the branch:

git branch -d feature-branch

This is the safe delete option. Git will only allow the deletion if the branch has been fully merged into your current branch. If you try to delete an unmerged branch this way, Git will warn you and refuse, protecting you from accidentally losing work.

Think of -d as asking for permission: "Git, may I delete this branch if it's safe to do so?"

Force Deletion with -D

Sometimes you intentionally want to discard a branch with unmerged work—maybe it was an experimental dead-end or a mistake. Use the uppercase -D flag:

git branch -D experimental-idea

This force deletes the branch regardless of merge status. Git will not ask questions or warn you. The commits become orphaned and will eventually be garbage-collected.

Think of -D as a command: "Git, delete this branch no matter what."

When to Use Each

Use -d as your default—it's the safety net that prevents accidental data loss. Only use -D when you're absolutely certain you want to throw away unmerged work.

Key Takeaway: Use git branch -d to safely delete merged branches, and git branch -D to force-delete branches with unmerged work you intentionally want to discard.