Friday, 12 December 2025

Git: Deleting old local branches

Just looking at old local branches. They tend to proliferate, especially if you have a release cadence.

To list the local branches (the default), or list the branches according to a pattern, see the two examples directly below.

git branch
git branch --list "V2022*"

Small explanation of the output of the command:

  • existing branches are listed
  • the current branch will be highlighted in green and marked with an asterisk
  • any branches checked out in linked worktrees will be highlighted in cyan and marked with a plus sign

I'm usually only interested in local branches, but "-r" shows remote-tracking branches and "-a" shows both remote and local branches, if you're interested.

Removing branches

git branch -D `git branch --list "V2022*"`

Bear in mind that branches that are used by a worktree cannot be deleted. Remove the worktree first.

Actually, I really like that behaviour.

Checking old branches

Apparently you can sort the list of branches based on last comitterdate.

git branch --sort=-committerdate # DESC

In the example above, you'll see branches that have been committed to recently at the top.

Finding a commit

This is nice, you can find which branches contain a certain commit quickly.

git branch --list --contains 089aafb331a08d19ed805fff6fea3846776980a0

Unfortunately, we're currently using git as a local repo, and svn remote, so there's a disconnect between commit hashes.

References

Git - git branch documentation
https://git-scm.com/docs/git-branch
StackOverflow - Can you delete multiple branches in one command with Git?
https://stackoverflow.com/questions/3670355/can-you-delete-multiple-branches-in-one-command-with-git