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: git branch --merged to find branches you can delete

Git
~/tips/tip-git-branch-merged-cleanup

git branch --merged main

Lists local branches already merged into main so you can delete them safely.

My local branch list only grows. Git can tell me which of those branches are already merged, so I know which ones are safe to delete.

The docs say --merged lists only branches whose tips are reachable from a commit, HEAD if you do not name one. Name your main branch to be explicit:

git branch --merged main

The output is a plain list. The current branch is in it too, marked with a star, so do not delete that one by reflex:

* main
  old-feature

Read the list, then delete what you recognise:

git branch -d old-feature

The -d variant is the careful one. Per the docs the branch must be fully merged in its upstream, or in HEAD if no upstream is set. Git refuses anything else:

error: the branch 'wip' is not fully merged.
If you are sure you want to delete it, run 'git branch -D wip'

Only -D forces it. It throws away commits that nothing else points to, so use it when you are sure.

Caveats: because -d compares against HEAD when there is no upstream, run it while you are on main. And a branch that was merged with a squash merge on the server will not show up, since its original commits are not reachable from main. For those, git branch --no-merged main lists what is left, and you check each one by hand before using -D.

Further reading: git-branch documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link