How do you find which branches are already merged into main?
Run git branch --merged main to list branches whose commits are all in main, and git branch --no-merged main for the rest. Merged ones are usually safe to delete.
Explain it simply
After a few months a repo collects branches like old exam notebooks. Before throwing any away you want to know which ones have been fully copied into the main file.
git branch --merged main lists branches whose tip is reachable from main, so deleting them loses nothing. --no-merged shows those still carrying unique work. Add -r for remote branches. One catch: squash-merged branches count as not merged, because the original commits never entered main.
Example
git branch --merged main
# feature/login
# fix/typo
git branch --no-merged main
# feature/reports
git branch --merged main | grep -v main | xargs git branch -dWhat to say in the interview
"git branch --merged main lists branches fully contained in main, which are safe to delete, and --no-merged lists those with unique commits. Branches merged by squash show up as unmerged, so I check those manually before deleting."
Common mistakes
- Trusting --merged for squash-merged pull requests.