| 1 | [](https://github.com/pre-commit/pre-commit) |
| 2 | [](https://github.com/hartwork/git-delete-merged-branches/actions/workflows/run-tests.yml) |
| 3 | [](https://repology.org/project/git-delete-merged-branches/versions) |
| 4 | |
| 5 | |
| 6 | # git-delete-merged-branches |
| 7 | |
| 8 | A convenient command-line tool helping you keep repositories clean. |
| 9 | |
| 10 | |
| 11 | # Installation |
| 12 | |
| 13 | ```console |
| 14 | # pip install git-delete-merged-branches |
| 15 | ``` |
| 16 | |
| 17 | If you are using one of the distributions below, you can install |
| 18 | `git-delete-merged-branches` through the respective package manager, e.g.: |
| 19 | |
| 20 | | Distribution | Command | |
| 21 | | ------------ | ------- | |
| 22 | | [Arch Linux (AUR)](https://aur.archlinux.org/packages/git-delete-merged-branches/) | `yay -S git-delete-merged-branches` | |
| 23 | | [Debian GNU/Linux (>=bookworm)](https://packages.debian.org/bookworm/git-delete-merged-branches) | `apt install git-delete-merged-branches` | |
| 24 | | [Gentoo Linux](https://packages.gentoo.org/packages/dev-vcs/git-delete-merged-branches) | `emerge -av dev-vcs/git-delete-merged-branches` | |
| 25 | | [Homebrew](https://formulae.brew.sh/formula/git-delete-merged-branches) | `brew install git-delete-merged-branches` | |
| 26 | | [MacPorts](https://ports.macports.org/port/py-git-delete-merged-branches/details/) | `port install py-git-delete-merged-branches` | |
| 27 | | [NixOS](https://search.nixos.org/packages?query=git-delete-merged-branches) | `nix-shell -p git-delete-merged-branches` | |
| 28 | | [Ubuntu (>=lunar/23.04)](https://packages.ubuntu.com/source/lunar/git-delete-merged-branches) | `apt install git-delete-merged-branches` | |
| 29 | | [Void Linux](https://github.com/void-linux/void-packages/tree/master/srcpkgs/git-delete-merged-branches) | `xbps-install -S git-delete-merged-branches` | |
| 30 | |
| 31 | |
| 32 | # Example |
| 33 | |
| 34 | ```console |
| 35 | # git-delete-merged-branches |
| 36 | Do you want to run "git remote update --prune" for 1 remote(s): |
| 37 | - origin |
| 38 | |
| 39 | Update? [y/N] y |
| 40 | |
| 41 | Do you want to run "git pull --ff-only" for 1 branch(es): |
| 42 | - master |
| 43 | |
| 44 | Pull? [y/N] y |
| 45 | |
| 46 | You are about to delete 6 local branch(es): |
| 47 | - improve-setup-py |
| 48 | - issue-12-enable-ci-for-pull-requests |
| 49 | - issue-5-fix-waste-of-one-second-per-service |
| 50 | - keep-github-actions-up-to-date |
| 51 | - refactoring-one |
| 52 | - simple-ci |
| 53 | |
| 54 | Delete? [y/N] y |
| 55 | |
| 56 | 6 local branch(es) deleted. |
| 57 | You are about to delete 6 remote branch(es): |
| 58 | - origin/improve-setup-py |
| 59 | - origin/issue-12-enable-ci-for-pull-requests |
| 60 | - origin/issue-5-fix-waste-of-one-second-per-service |
| 61 | - origin/keep-github-actions-up-to-date |
| 62 | - origin/refactoring-one |
| 63 | - origin/simple-ci |
| 64 | |
| 65 | Delete? [y/N] y |
| 66 | To github.com:hartwork/wait-for-it.git |
| 67 | - [deleted] improve-setup-py |
| 68 | - [deleted] issue-12-enable-ci-for-pull-requests |
| 69 | - [deleted] issue-5-fix-waste-of-one-second-per-service |
| 70 | - [deleted] keep-github-actions-up-to-date |
| 71 | - [deleted] refactoring-one |
| 72 | - [deleted] simple-ci |
| 73 | |
| 74 | 6 remote branch(es) deleted. |
| 75 | ``` |
| 76 | |
| 77 | |
| 78 | # Features |
| 79 | |
| 80 | - Supports deletion of both local and remote branches |
| 81 | - Detects multiple forms of de-facto merges |
| 82 | (rebase merges, |
| 83 | squash merges (needs `--effort=3`), |
| 84 | single or range cherry-picks… |
| 85 | leveraging `git cherry`) |
| 86 | - Supports workflows with multiple release branches, e.g. only delete branches that have been merged to *all* of `master`, `dev` and `staging` |
| 87 | - Quick interactive configuration |
| 88 | - Provider agnostic: Works with GitHub, GitLab, Gitea and any other Git hosting |
| 89 | - Takes safety seriously |
| 90 | |
| 91 | |
| 92 | # Safety |
| 93 | |
| 94 | Deletion is a sharp knife that requires care. |
| 95 | While `git reflog` would have your back in most cases, |
| 96 | `git-delete-merged-branches` takes safety seriously. |
| 97 | |
| 98 | Here's what `git-delete-merged-branches` does for your safety: |
| 99 | - No branches are deleted without confirmation or passing `--yes`. |
| 100 | - Confirmation defaults to "no"; plain `[Enter]`/`[Return]` does not delete. |
| 101 | - `git push` is used with `--force-with-lease` so if the server and you have a different understanding of that branch, it is not deleted. |
| 102 | - There is no use of `os.system` or shell code to go wrong. |
| 103 | - With `--dry-run` you can get a feel for the changes that `git-delete-merged-branches` would be making to your branches. |
| 104 | - Show any Git commands run using `--verbose`. |
| 105 | |
| 106 | |
| 107 | # Best Practices |
| 108 | |
| 109 | When the repository is a fork |
| 110 | (with an upstream remote and a fork remote): |
| 111 | |
| 112 | - Make sure that release branches are tracking the right remote, |
| 113 | e.g. `master` should probably track original upstream's `master` |
| 114 | rather than `master` of your fork. |
| 115 | - Consider keeping your fork's remote `master` up to date (using `git push`). |
| 116 | |
| 117 | When using GitHub: |
| 118 | |
| 119 | - You can make GitHub |
| 120 | [delete branches of merged pull requests](https://docs.github.com/en/github/administering-a-repository/managing-the-automatic-deletion-of-branches) |
| 121 | for you, to save `git-delete-merged-branches` some work on the remote side, if you like. |
| 122 | |
| 123 | |
| 124 | # Name clash on "git-delete-merged-branches" with git-extras |
| 125 | |
| 126 | Utility collection [git-extras](https://github.com/tj/git-extras) provides a |
| 127 | [simple shell script named `git-delete-merged-branches`](https://github.com/tj/git-extras/blob/master/bin/git-delete-merged-branches) |
| 128 | that competes for command `git-delete-merged-branches` when both software packages are installed. |
| 129 | |
| 130 | To address this conflict you can: |
| 131 | |
| 132 | - a) Invoke `python3 -m git_delete_merged_branches` instead of `git-delete-merged-branches` |
| 133 | - b) Invoke command `git-dmb` instead of `git-delete-merged-branches` |
| 134 | - c) Control path resolution order through environment variable `$PATH` |
| 135 | - d) Invoke `git-delete-merged-branches` with its absolute path. |
| 136 | You can run `which -a git-delete-merged-branches` to see where it's located. |
| 137 | |
| 138 | |
| 139 | # Support |
| 140 | |
| 141 | Please report any bugs that you find. |
| 142 | |
| 143 | Like this tool? Support it with a star! |