Course contentsShow
← Software Engineering
Lesson 63 of 1,9332. Version Control with GitFree lesson

Deleting and Cleaning Up Branches

Learn to delete merged branches locally and remotely to keep your repository clean and organized.

Deleting and Cleaning Up Branches

You'll learn how to remove branches you no longer need, keeping your repository tidy and easy to navigate.

Once you've merged a feature branch into your main branch, that branch has served its purpose. Leaving old branches around clutters your repository—imagine a filing cabinet stuffed with folders labeled "Already Filed Elsewhere." It becomes harder to find active work and understand what's actually in progress.

Why Delete Merged Branches?

After a successful merge, the branch pointer is just a label pointing to commits that are now part of your main history. The commits themselves remain safe in the repository's history. Deleting the branch removes only the label, not the work.

Deleting a Local Branch

Use git branch -d to delete a branch that's been merged:

git branch -d feature-login

Git will prevent you from deleting a branch with unmerged work (a safety feature). If you're absolutely sure you want to force deletion, use -D instead—but be careful, as this discards unmerged changes.

Viewing All Branches

Before cleaning up, check what branches exist:

git branch

This lists all local branches. The current branch is marked with an asterisk (*).

When to Clean Up

Delete branches after they've been successfully merged and you're confident you won't need the branch label anymore. Many teams delete feature branches immediately after merging. Think of it like throwing away sticky notes once you've completed the tasks written on them.

Key Takeaway: Deleting merged branches with git branch -d keeps your repository organized by removing unnecessary labels while preserving the actual commit history—it's housekeeping, not destruction.