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-loginGit 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-loginYou cannot delete the branch you are currently on. Switch away first, then delete:
git switch main
git branch -d feature-loginDeleting 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
EasyThe 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-loginForce-delete a branch with unmerged commits:
git branch -D feature-loginYou must switch away from a branch before deleting it:
git switch main
git branch -d feature-loginDeleting a branch removes the pointer, not the commits themselves.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Version Control
7Branching
What Is A BranchCreating A BranchSwitching BranchesCommitting On A BranchDeleting A BranchRecap - Branch Play2Getting Started
Initialize A RepositoryThe .git FolderConfigure Your IdentityGit StatusRecap - First Repo8Merging
What Is A MergeFast-Forward MergeThree-Way MergeMerge ConflictsResolve A ConflictRecap - Merge Master11Feature Branch Project
Project OverviewInitialize Main3Tracking Changes
The Staging AreaGit AddGit CommitModifying A Tracked FileGit LogRecap - First Commits6Recipe Site Project
Project OverviewInitialize And Ignore