Skip to content

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 main first.

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 -d until you are sure the work is saved on main or 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-report with your real branch name from git 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 reflog soon afterward, or get help before the history ages out.

This content repository is based on the hands-on experience of Naveen Bachwani. AI tools were also used in augmenting and refining it. The goal is to help non-technical users get familiar with these tools, so they may be able to use them effectively.

Readers are responsible for using any commands or instructions provided here with appropriate caution. The creator of this site accepts no responsibility for system damage, data loss, or other consequences resulting from their use.