Menu
Coddy logo textTech

Deleting A Branch

Part of the Version Control section of Coddy's Terminal journey — lesson 32 of 58.

When a branch is no longer needed (e.g., the work has been merged, or the experiment was abandoned), delete it with the -d flag:

git branch -d feature-login

Git refuses to delete a branch that has unmerged commits. This is a safety check; it prevents you from losing work by mistake.

If you really do want to throw away the branch and its commits, force the delete with -D:

git branch -D feature-login

You cannot delete the branch you are currently on. Switch away first, then delete:

git switch main
git branch -d feature-login

Deleting a branch removes the pointer, not the commits themselves. As long as another branch or tag still references those commits, they remain in the repo.

challenge icon

Challenge

Easy

The repo has commits on main and a leftover branch called old-feature that points to the same commit as main (so it has no unmerged work).

Delete old-feature with the safe -d flag, then list all remaining branches.

Cheat sheet

Delete a merged branch (safe):

git branch -d feature-login

Force-delete a branch with unmerged commits:

git branch -D feature-login

You must switch away from a branch before deleting it:

git switch main
git branch -d feature-login

Deleting a branch removes the pointer, not the commits themselves.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Version Control