Branch Cleanup Hygiene
Six real branches, three real categories. Clean up every single one with the right real command.
This block has left real, genuine branches scattered across the project: two already merged and simply never deleted, one abandoned experiment, one genuinely stale refactor, one real feature still honestly in progress, and one whose remote copy someone already deleted without you knowing.\n\nReal branch hygiene means treating each of these differently, not deleting everything in sight or ignoring all of it forever. git fetch --prune, git branch --merged, and git branch --no-merged turn a confusing pile into three real, distinct categories — each with its own correct real cleanup command.
Discovering an Orphaned Branch
git fetch --prune origin
git branch -vv
--prune removes remote-tracking references for branches already deleted on the remote. A real ": gone" annotation on git branch -vv marks a local branch whose remote copy no longer exists.
Merged vs Unmerged, For Real
git branch --merged master
git branch --no-merged master
--merged lists branches whose every commit is already reachable from master — genuinely safe to delete. --no-merged lists the ones that still hold real, unique work.
Deleting What's Genuinely Safe
git branch -d feature/vegan-notes feature/gluten-free-notes experiment/new-idea
git push origin --delete feature/vegan-notes feature/gluten-free-notes experiment/new-idea
git branch -d (lowercase) genuinely refuses to delete a branch with unmerged work — real, built-in protection.
Force-Deleting What's Deliberately Abandoned
git branch -D feature/abandoned-idea
git branch -D feature/old-refactor
git push origin --delete feature/old-refactor
-D (capital) genuinely overrides that protection — reserved for branches a real, deliberate decision has already abandoned.
git fetch --prune
Removes local remote-tracking references for branches already deleted on the remote. Confirmed directly in this lab: it reported "[deleted] (none) -> origin/feature/abandoned-idea", and the local branch subsequently showed ": gone" in branch -vv.
git branch --merged / --no-merged
Lists branches whose commits are (or are not) all already reachable from the given branch. Confirmed directly in this lab: --merged master correctly identified the two already-integrated feature branches plus a trivial, content-free experiment branch, while --no-merged correctly flagged the genuinely unfinished and abandoned ones.
git branch -D (force delete)
Deletes a branch regardless of whether its commits are merged anywhere — genuinely overriding the safety check plain -d performs. Confirmed directly in this lab: -d alone would have refused the unmerged abandoned and old-refactor branches; -D deleted them anyway, as a deliberate real decision.
🔎 Discover the Orphaned Branch
Prune stale remote-tracking references, and spot the branch whose remote copy is already gone.
cd ~/team-recipes && git fetch --prune origincd ~/team-recipes && git branch -vvstudent@lab:~$ cd ~/team-recipes && git fetch --prune origin From ../recipes-origin - [deleted] (none) -> origin/feature/abandoned-idea student@lab:~$ cd ~/team-recipes && git branch -vv experiment/new-idea d891fe9 [origin/experiment/new-idea] Add hot chocolate recipe feature/abandoned-idea 3586da2 [origin/feature/abandoned-idea: gone] Start an idea that will be abandoned feature/dessert-menu be66915 [origin/feature/dessert-menu] Merge remote-tracking branch 'origin/master' into feature/dessert-menu feature/gluten-free-notes c146c5b [origin/feature/gluten-free-notes] Mark dietary notes as gluten-free friendly feature/old-refactor 8f80026 [origin/feature/old-refactor] WIP refactor attempt 6 feature/vegan-notes c8fd194 [origin/feature/vegan-notes] Mark dietary notes as vegan-friendly * master da919ea [origin/master] Merge pull request: gluten-free notes
📊 Sort Merged From Unmerged, For Real
Get git's own real answer for which branches are safe, and which need a decision.
cd ~/team-recipes && git branch --merged mastercd ~/team-recipes && git branch --no-merged masterstudent@lab:~$ cd ~/team-recipes && git branch --merged master experiment/new-idea feature/gluten-free-notes feature/vegan-notes * master student@lab:~$ cd ~/team-recipes && git branch --no-merged master feature/abandoned-idea feature/dessert-menu feature/old-refactor
🗑️ Delete Everything Genuinely Safe
Remove the three merged branches, locally and on the remote.
cd ~/team-recipes && git branch -d feature/vegan-notes feature/gluten-free-notes experiment/new-ideacd ~/team-recipes && git push origin --delete feature/vegan-notes feature/gluten-free-notes experiment/new-ideastudent@lab:~$ cd ~/team-recipes && git branch -d feature/vegan-notes feature/gluten-free-notes experiment/new-idea Deleted branch feature/vegan-notes (was c8fd194). Deleted branch feature/gluten-free-notes (was c146c5b). Deleted branch experiment/new-idea (was d891fe9). student@lab:~$ cd ~/team-recipes && git push origin --delete feature/vegan-notes feature/gluten-free-notes experiment/new-idea To ../recipes-origin.git - [deleted] experiment/new-idea - [deleted] feature/gluten-free-notes - [deleted] feature/vegan-notes
⚠️ Force-Delete the Deliberately Abandoned
The orphaned experiment and the stale refactor are real, deliberate abandons. Force-delete both, and confirm the final clean state.
cd ~/team-recipes && git branch -D feature/abandoned-ideacd ~/team-recipes && git branch -D feature/old-refactorcd ~/team-recipes && git push origin --delete feature/old-refactorcd ~/team-recipes && git branch -astudent@lab:~$ cd ~/team-recipes && git branch -D feature/abandoned-idea Deleted branch feature/abandoned-idea (was 3586da2). student@lab:~$ cd ~/team-recipes && git branch -D feature/old-refactor Deleted branch feature/old-refactor (was 8f80026). student@lab:~$ cd ~/team-recipes && git push origin --delete feature/old-refactor To ../recipes-origin.git - [deleted] feature/old-refactor student@lab:~$ cd ~/team-recipes && git branch -a feature/dessert-menu * master remotes/origin/HEAD -> origin/master remotes/origin/feature/dessert-menu remotes/origin/hotfix/soup-typo remotes/origin/master
Lab 2.2.4 complete — Block 2.2 complete. Six real branches, three real categories, all genuinely resolved:\n\n\n Orphaned branch discovered : ✅ fetch --prune\n Merged vs unmerged sorted : ✅ real, correct split\n Safe branches deleted : ✅ -d, no complaints\n Abandoned branches removed : ✅ -D, deliberate\n
Enable JavaScript to run the live terminal and track your progress.