Git Branching
Last updated 2026-08-20 · 4 notes · 3 tags
Tags: git branches cleanup
Branches are parallel lines of work. When a feature is finished and merged, delete the old branch so the list stays tidy.
Note: You cannot delete the branch you are currently on. Switch to
mainfirst.
1. Switch away from the branch
git switch main
2. Delete a local branch
Safe delete (only if already merged):
git branch -d branch-name
Force delete (even if not merged):
git branch -D branch-name
Note: Prefer
-duntil you are sure the work is saved onmainor GitHub.
3. Delete the branch on GitHub (origin)
git push origin --delete branch-name
4. Delete local and remote
git branch -D branch-name
git push origin --delete branch-name
5. List branches
git branch
git branch -r
git branch -a
6. Remove stale remote-tracking names
If GitHub already deleted a branch but your PC still lists origin/old-name:
git fetch --prune
or:
git remote prune origin
Note: This only cleans labels on your machine. It does not delete files in your project folder.
Example
Delete a finished branch named feature/new-report:
git switch main
git branch -D feature/new-report
git push origin --delete feature/new-report
git fetch --prune
Note: Replace
feature/new-reportwith your real branch name fromgit branch.
Common mistakes
- Deleting a branch does not delete commits that were already merged into
main. - If you force-deleted a branch too early, check
git reflogsoon afterward, or get help before the history ages out.
This project chronicles ideas, decisions and lessons I have gleaned from building with AI. The goal is to help non-technical users get familiar with these tools and techniques, so that they may be able to use them effectively. At times, I have also made the use of AI tools to augment or refine the content.
Readers are responsible for using any commands or instructions published here with due caution. The creator of this site accepts no responsibility for any system damage, data loss or other consequences, resulting from the application of this content.