Skip to main content

git-prune-merged

Git plugin to help you keep repository clean from the command line, by pruning local and remote branches that have been integrated in GitHub.

Installation

The package is available through PyPI.

pip install git-prune-merged

Basic usage

First you need to configure GitHub connection. The recommended way to authenticate is to use a personal access token. Also, the name of the repository where you are going to create your Pull Requests is required. Usually, this is the repository in which you are currently working, but in forked repositories this needs to be the upstream repository name.

git prune-merged config -- token <token_id> -- repo 'repo/name'

Print the help to get a glimpse of all the commands, arguments and flags:

git prune-merged -h

List all branches that would be pruned. Make sure to have fetched your remote before running 'prune-merged' if you have recently integrated a branch that you expect to be pruned.

$ git prune-merged ls
[I] There are 5 local branches that can be pruned:
[I]     dev-branch1
[I]     dev-branch2
[I]     dev-branch3
[I]     dev-branch4
[I]     dev-branch5
[I] There are 4 remote branches that can be pruned:
[I]     origin/dev-branch1
[I]     origin/dev-branch2
[I]     origin/dev-branch3
[I]     origin/dev-branch4

List branches older than 3 months:

$ git prune-merged ls --months 3
[I] No local branches to prune.
[I] No remote branches to prune.

Prune all remote branches:

$ git prune-merged -r
[I] Going to remove origin/dev-branch1
    Are you sure? [Y/n]
[I] [deleted] .... origin/dev-branch1
[I] Going to remove origin/dev-branch2
    Are you sure? [Y/n] y
[I] [deleted] .... origin/dev-branch2
[I] Going to remove origin/dev-branch3
    Are you sure? [Y/n] n
[I] Skipping origin/dev-branch3
[I] Going to remove origin/dev-branch4
    Are you sure? [Y/n] n
[I] Skipping origin/dev-branch4
[I] All (2) remote branches pruned.

Do a dry-run simulation for prunning all branches (local and remote), and do not ask for confirmation:

$ git prune-merged --all --yes --dry-run
[I] [deleted] .... origin/dev-branch3
[I] [deleted] .... origin/dev-branch4
[I] All (2) remote branches pruned.
[I] [deleted] .... dev-branch1
[I] [deleted] .... dev-branch2
[I] [deleted] .... dev-branch3
[I] [deleted] .... dev-branch4
[I] [deleted] .... dev-branch5
[I] All (5) local branches pruned.

How it works

A branch that has been integrated by rebasing in GitHub will update the SHAs of the commits, making it difficult to track whether a branch has been merged, since the SHAs will differ.

Although branches can be automatically deleted after merge, making it easier to manage these cases (e.g., git garbage collector), it is not always posible or desirable to change the configuration of a project.

Currently, this plugin works only in GitHub-hosted repositories. The plugin discerns whether a branch has been merged by exploring the user's GitHub pull requests (PR). By looping through all the 'closed and merged' PRs for the user, and comparing the HEAD SHA against the local and remote HEAD SHAs, it is able to discern which branches can be safely pruned. Using SHAs is a secure way to do this, since SHA is unique. Using branch name could lead to wrongfully pruned branches.

Safety measures

  • Never remove a branch without a confirmation or the --yes flag.
  • Posibility to print all branches that will be pruned before running the command.
  • Dry-run option to safely pre-verify what would be removed.
  • Using SHAs to ensure that the branches that will be pruned are the ones that were integrated.
  • Allow to prune only older branches to month granularity.

Support

Please report any bugs, issues or suggestions you may have.

Like this plugin? Support it with a star!

Metadata

Release files for git-prune-merged 0.4.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for git-prune-merged 0.4.6
File Size Uploaded
git-prune-merged-0.4.6.tar.gz 19.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for git-prune-merged 0.4.6
File Interpreter ABI Platform
git_prune_merged-0.4.6-py3-none-any.whl Python 3 none any Details

Total release size: 42.0 kB

Release files / git-prune-merged-0.4.6.tar.gz

Download URL git-prune-merged-0.4.6.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
44d8de7c41fae4e0d1cd73a7a42de0955a323676974ab8fa8e7f361937e1407c
BLAKE2b-256 checksum
How to use checksums
7752689aa5db6ffa960683cd03354ce2d5da71515f86840f7315ee569188ad05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / git_prune_merged-0.4.6-py3-none-any.whl

Download URL git_prune_merged-0.4.6-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
991d7409090f06f50b2785f924503fad5e66adc794b562bdb6a37351ea65225f
BLAKE2b-256 checksum
How to use checksums
5ef8c148b25af02f8316656367f6811c04e368a24c8f3828a3d7e3ef392f3199
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

This release

0.4.6 This release

2 release files

0.4.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page