Skip to content

patik/git-prune-branches

Repository files navigation

git-prune-branches

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:

Demo

Why?

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.

What does it do?

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.

Prompt with list of branches, allowing arbitrary selection

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.

Installation

NPM

npm install -g git-prune-branches

Please install a package globally with -g flag so that you can use it directly as a sub command of git, like this:

git prune-branches

NPX

It's also possible to use package through npx without installing:

npx git-prune-branches

Usage

git prune-branches

This 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.

Confirmation prompt

Auto-removal

To delete all local branches without choosing which ones, and without confirmation, use --prune-all or -p flag

git prune-branches --prune-all

This command will compare your local branches to the remote ones and remove, those which do not exist anymore on the remote side.

Custom remote

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 upstream

Forcing removal

If 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 --force

Retrying with --force

If 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.

Skipping confirmation

You can skip the confirmation prompts with --yes or the shortcut -y:

git prune-branches -y

Version

To print the version:

git prune-branches --version

Troubleshooting

If 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-branches

Development

Testing

This project uses Vitest for testing. The tests create a temporary git repository and verify the behavior of the tool in different scenarios.

Run tests in watch mode

pnpm test

Run tests once

pnpm test:once

Building

Build the TypeScript source:

pnpm build

Linting and Formatting

pnpm lint
pnpm format

Credit

Forked from git-removed-branches by Maks Nemisj @nemisj

About

Find and remove local git branches which were deleted from the remote

Resources

License

Stars

Watchers

Forks

Packages

No packages published