Skip to main content
Chapter 60 of 61 · 1 sections · Page 186

Git: Tidying Local & Remote Repo

Prune dead remote tracking branches and clean up deleted local branches using git fetch -p.

Original chapter reference: Chapter 60: Tidying up your local and remote repository. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Section 60.1: Delete local branches that have been deleted on the remote

To remote tracking between local and deleted remote branches use

Source example · PDF page 186
git fetch -p

you can then use

Source example · PDF page 186
git branch -vv

to see which branches are no longer being tracked.

Branches that are no longer being tracked will be in the form below, containing 'gone'

Source example · PDF page 186
 branch               12345e6 [origin/branch: gone] Fixed bug

you can then use a combination of the above commands, looking for where 'git branch -vv' returns 'gone' then using '-d' to delete the branches

Source example · PDF page 186
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d
Buy me a coffee
Daily Practice
0/0