brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: fetch.prune cleans up branches that no longer exist remotely

Git settings and fixes
~/tips/tip-git-fetch-prune

git fetch --prune

Removes stale remote-tracking branches every time you fetch.

Your branch list keeps growing with remote branches that were deleted a long time ago. Git does not remove them unless you ask.

git fetch --prune
git config --global fetch.prune true

A fetch only adds. When someone deletes a branch on the remote after a merge, your origin/that-branch reference stays. --prune removes remote-tracking references that no longer exist on the remote, before it fetches. The config variable fetch.prune makes every fetch (and every pull) behave as if you had typed the flag.

After that, git branch -r only shows branches that are really there.

The caveat is what it does not touch. It only removes the remote-tracking references. Your own local branches stay, even if the remote branch is gone. For those, git branch --merged lists the local branches that are fully contained in your current branch. The docs say those can be deleted safely.

git branch --merged main

Check that list before you delete anything, and keep main out of it. Local branches that were merged by squash can look unmerged to git, so a branch missing from the list is not proof that it still has work in it. Also, tags are mostly left alone by pruning when they only come along by auto-following.

Further reading: git fetch documentation, git branch documentation, On this site: sort your branches by last commit with branch.sort, On this site: set push.autoSetupRemote and stop typing –set-upstream

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Git settings and fixes tips git merge --abort concurrency: git rev-parse --show-toplevel git config --get-regexp '^user\.' git commit -C ORIG_HEAD All Git settings and fixes tips (40) All topics

Link