⑨ lab ≡ who cares?!

Git/Pruning Branches

A key git mantra is that branches are cheap. Hence GitLab, GitHub, and whatever else is out there recommend to develop every little feature, fix, etc. in a branch.

Branches in GitLab and GitHub are needed to create merge/pull requests that can go through the code review process including any pipelines that need to complete before one can merge to the main development line.

If you are a frequent code reviewer, work in a larger team, and prefer reviewing code in your local development environment, you end up with a ton of checked out branches. The below rc script was designed to prune any branches that have been deleted upstream, cleaning up any clutter that has accumulated locally.

 % cat $home/bin/git/prune
# Delete stale references associated with <name>
  git remote prune origin >[2]/dev/null

# Iterate over all refs that match <refs/heads> and show them
# according to the given <format> matching lines ending in
# /\[gone\]$/, extracting their branch name using awk
  git for-each-ref --format '%(refname) %(upstream:track)' refs/heads \
    | 9 awk '/\[gone\]$/  {sub("refs/heads/", "", $1); print $1}' -

echo 'gitprune: Pruning '^$#git_gone_upstream^' branches'.

for(branch in $git_gone_upstream) {
  object_SHA1=`{git rev-parse --verify --quiet $branch}
  if (~ $status '') {
    git branch -D $branch

Here is what the output looks like for a fictional repository:

 % git/prune
gitprune: Pruning 2 branches.
Deleted branch feature/nein-nein-nein-999 (was c4a4e8170).
Deleted branch hotfix/P10019563-48255 (was c0965bc75).

The above script requires plan9port, however, it should hopefully be trivial to port it to your favourite shell.

#Dev #Plan9Port