Menu
CoddyTech

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

Set up the repository, make a commit on main, then create a branch called old-feature at that same commit (so it has no unmerged work).

Every run starts from a fresh copy of the folder, and the whole script in the editor runs again from the top. Nothing is committed for you: begin with git init -q -b main, set git config user.name and git config user.email, and commit the starting file. Keep those lines in the editor. If you clear them you will see fatal: not a git repository.

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

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

Practice on your own: Terminal playground