Giter Club home page Giter Club logo

git-prune-merged's Introduction

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, using its REST API to explore the remotes. The plugin discerns whether a branch has been merged by looking for 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.

Inspiration

This project is partially inspired by git-delete-merged-branches.

Despite taking a different approach, git-prune-merged and git-delete-merged-branches are similar solutions that can help keeping your repository clean of old branches.

Support

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

Like this plugin? Support it with a star!

git-prune-merged's People

Contributors

aesteve-rh avatar

Stargazers

 avatar

Watchers

 avatar

git-prune-merged's Issues

Add exclusion list

Maybe interesting to have an exclusion list of branches you don't want to be pruned, in order to safely invoke with --all.

Add --years option

Simple filter to prune only older branches.

For example:

git prune-rebase pr --years 1 would prune all branches older than 1 year.

Unify list and prune commands

We can keep config, but list and prune can be probably unified/removed.

If launched without command, just execute a prune, but print the list of branches that will be pruned first.

Add --dry-run option

Add option to do a prune without actually deleting branches, for increased safety.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.