Interview Prep/Git & GitHub/Branching and Merging Workflows

Your repo has 300 stale branches locally and remotely. How do you clean up safely?

BeginnerScenarioBranching and Merging Workflows
Answer

Situation read → Merged branches are safe to delete, others need a check.

What I'd do →

  1. git fetch --prune to drop stale remote-tracking refs.
  2. List merged local branches: git branch --merged main, and delete with git branch -d.
  3. Delete a remote branch: git push origin --delete old-branch.
  4. Turn on auto-delete of head branches in GitHub and set fetch.prune true.
git branch --merged main | grep -vE '^\*|main|develop' | xargs -r git branch -d

Why → -d refuses unmerged branches, giving a safety net.

Interview tip → Squash-merged branches will not show in --merged; cross-check with the closed PR list.

All Git & GitHub questions Roadmap: Git & GitHub

More on Branching and Merging Workflows