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
EasySet 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, setgit config user.nameandgit config user.email, and commit the starting file. Keep those lines in the editor. If you clear them you will seefatal: not a git repository.
Delete old-feature with the safe -d flag, then list all remaining branches.
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 IgnorePractice on your own: Terminal playground