not-an-aardvark / git-delete-squashed Goto Github PK
View Code? Open in Web Editor NEWDelete branches that have been squashed and merged into main
License: MIT License
Delete branches that have been squashed and merged into main
License: MIT License
currently it seems compare with local master, but maybe there is some difference between local/remote master.
Hi! Thanks for this ๐. I would love to see a --dry-run
option so that I can see what will be deleted before going ahead. Would that be feasible?
The version on npm is outdated and isn't updated with the latest change that allows branch name specification. Manual clone and install works but not everyone will know that. Can we have an NPM update?
i added this git alias, per docs:
$ git config --global alias.delete-squashed '!f() { local targetBranch=${1:-main} && git checkout -q $targetBranch && git branch --merged | grep -v "\*" | xargs -n 1 git branch -d && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base $targetBranch $branch) && [[ $(git cherry $targetBranch $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done; }; f' with bin/bash
But it's failing for some branches, probably since [[
is disabled when run as sh
.
git delete-squashed
Deleted branch my-branch (was d7be4206).
f() { local targetBranch=${1:-main} && git checkout -q $targetBranch && git branch --merged | grep -v "\*" | xargs -n 1 git branch -d && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base $targetBranch $branch) && [[ $(git cherry $targetBranch $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done; }; f: 1: [[: not found
f() { local targetBranch=${1:-main} && git checkout -q $targetBranch && git branch --merged | grep -v "\*" | xargs -n 1 git branch -d && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base $targetBranch $branch) && [[ $(git cherry $targetBranch $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done; }; f: 1: [[: not found
f() { local targetBranch=${1:-main} && git checkout -q $targetBranch && git branch --merged | grep -v "\*" | xargs -n 1 git branch -d && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base $targetBranch $branch) && [[ $(git cherry $targetBranch $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done; }; f: 1: [[: not found
Using Ubuntu 22.04 (bin/bash).
I had to run the npm test
/npm run lint
script manually to catch errors. It would be good if there was a CI step to run this check on each pull request.
Running this on a very large repository (hundreds of thousands of commits) results in an EAGAIN
error from spawning a process. This is probably because git-delete-squashed
exceeds the user process limit from spawning so many parallel git
processes.
There are a few potential ways to solve this:
git
processes so that only a limited number can exist at a timegit
)git
processes at all. (This would also improve performance because there wouldn't be so much process overhead.)Some repos now use the name main instead of master. This project seems to assume that the primary branch is named master. I'd like to request that the name of the primary branch be customizable.
For example:
$ git-delete-squashed --into main
$ git-delete-squashed --into primary
$ git-delete-squashed --into dev
possibly related to #1
When I run this tool via node on a big repo, I'm getting EAGAIN error on git rev-parse or merge-base
Error is like below (branch name is different every time I run the command)
git-delete-squashed
events.js:377
throw er; // Unhandled 'error' event
^
Error: spawn git EAGAIN
at Process.ChildProcess._handle.onexit (internal/child_process.js:274:19)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:280:12)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
errno: -35,
code: 'EAGAIN',
syscall: 'spawn git',
path: 'git',
spawnargs: [ 'merge-base', 'master', 'mynicebranch' ]
}
If a merge commit has been applied to master since a local branch was created, and the parents in the merge commit have no common ancestors (e.g. after a subtree merge), git-delete-squashed
crashes with an error:
fatal: ambiguous argument 'aaaaaaa^': unknown revision or path not in the working tree.
(where aaaaaaa
is the commit hash of the root commit from the merged branch).
For example:
Now the approach this script uses (git cherry
) fails because there is not a single commit that matches the squashed commit representing "develop-B", it's spread across two commits.
I think that it's possible to fix this and simplify the script by using git merge-tree
and git merge-base
. Here's an example: https://github.com/ggilder/dotfiles/blob/master/bin/git-delete-merged-branches#L9
Get the following error:
zsh: no matches found: master^e
fatal: 200af0b2ff7f00000000000000000000200af0b2 is not a valid object
zsh: no matches found: rick-jones^e
fatal: 1075c4a1fe7f000000000000000000001075c4a1 is not a valid object
zsh: no matches found: rickjones/ch3576/create-ruby-client-library^e
fatal: 006bf092fd7f00000000000000000000006bf092 is not a valid object
zsh: no matches found: rickjones/ch3759/create-endpoints-on-payment-service-for-new^e
fatal: f0e65a70ff7f00000000000000000000f0e65a70 is not a valid object
zsh: no matches found: rickjones/ch4459/extend-ruby-client-to-fetch-loan-information^e
fatal: 10f0d693fe7f0000000000000000000010f0d693 is not a valid object
zsh: no matches found: rickjones/ch4477/fix-create-cbpf-loan^e
fatal: a054e4d4fe7f00000000000000000000a054e4d4 is not a valid object
zsh: no matches found: rickjones/ch4506/make-create-loan-rate-dynamic^e
fatal: 20c79a2bfc7f0000000000000000000020c79a2b is not a valid object
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.