How I Git
Living Artifact
This is basically so I don't forget things.
Pruning local branches
I use git branch a lot to check branch names before switching to them, and I really hate it when the list gets clogged up with 20+ old branches.
Git has a native way to prune stale remote-tracking branches:
git fetch --prune
But that only removes the remote references - like origin/old-branch - it doesn't delete the actual branch, even if the remote branch it was tracking no longer exists.
So to clean those up, I use this:
git fetch --prune && \
git branch --format='%(refname:short) %(upstream:track)' | \
awk '$2 == "[gone]" {print $1}' | \
xargs git branch -d
git fetch --pruneupdates your remote-tracking information and removes stale remote branch references.git branch --format='%(refname:short) %(upstream:track)'lists local branches with just their name and upstream tracking status.awk '$2 == "[gone]" {print $1}'finds branches whose upstream branch has gone away, then prints the local branch name.xargs git branch -dpasses those branch names to Git and deletes them. If you want to delete branches that aren't fully merged, swap-dfor-D.