List or remove local tracked branches, which are deleted from the remote.
It's a fork of git-removed-branches with an interactive prompt, and the ability to retry deleting branches with --force
Addresses questions, like:
- Remove tracking branches no longer on remote
- How to prune local tracking branches that do not exist on remote anymore?
Because I'm tired of doing every time git fetch -p, git branch -r, git branch and keep comparing which branches are gone from the GitHub, but still available locally and doing git branch -D ${branch_name} on each of them, one by one.
This command will compare your local branches with remote and show you branches that are no longer available on remote but are still presented in your local repository. You can also use it to view and delete all (remotely) removed branches in one go using --prune-all flag.
This command works without the need to run git fetch -p, but a working network connection to your remote is required. If no connection can be established with the remote repository, then local information about your remote will be used instead. If your local repository is not in sync with the remote repository, it will warn you about it.
npm install -g git-prune-branchesPlease install a package globally with -g flag so that you can use it directly as a sub command of git, like this:
git prune-branchesIt's also possible to use package through npx without installing:
npx git-prune-branchesgit prune-branchesThis command will look through the branches that are no longer available on the remote and display them.
In case you haven't run git fetch -p, it will warn you to do so.
To delete all local branches without choosing which ones, and without confirmation, use --prune-all or -p flag
git prune-branches --prune-allThis command will compare your local branches to the remote ones and remove, those which do not exist anymore on the remote side.
If you have configured remote alias to something different than 'origin', you can use --remote or -r flag to specify the name of the remote. e.g., to specify remote to be upstream, you can use:
git prune-branches --remote upstreamIf you get an error when trying to delete branches:
The branch {branch_name} is not fully merged.you can force deletion by using --force flag or the -f alias
git prune-branches --prune-all --forceIf any branches fail to delete when the --force flag is not used, git-prune-branches will offer to retry and delete them again using --force.
You can skip the confirmation prompts with --yes or the shortcut -y:
git prune-branches -yTo print the version:
git prune-branches --versionIf you encounter error ERR_CHILD_PROCESS_STDIO_MAXBUFFER it is possible that your repository contains too many branches (more then 3382—see discussion).
You can fix this by specifying NODE_MAX_BUFFER environment variable. For example:
NODE_MAX_BUFFER=1048576 git prune-branchesThis project uses Vitest for testing. The tests create a temporary git repository and verify the behavior of the tool in different scenarios.
pnpm testpnpm test:onceBuild the TypeScript source:
pnpm buildpnpm lint
pnpm formatForked from git-removed-branches by Maks Nemisj @nemisj
