-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLI command to clean up after a merged PR #380
Comments
Thanks for so clearly laying out the steps you're taking today @jglick, this is really helpful. I marked this as a potential enhancement that we need to think about the UX around some. This isn't one of the next few things we'll get to but it definitely feels like it has the potential to alleviate that pain. |
If I'm not owner of the repository and am using fork, I also have to push (well, I don't have to but keeping the fork up-to-date is always good to have) updated base branch after pulling from upstream so would be great to have that available too. |
I do the same thing as well, using this script:
It would be very nice to automate this! |
@francois I don't think that works with squash merging though, right? |
You’re right, @jack1142, my script wouldn’t be able to detect a squash, only straight merges. gh should be able to do the cleanup itself, such as a hypothetical gh pr merge --cleanup, or the proposed gh pr cleanup.
|
Is it worth considering this alongside #373? I.e. can we have one command for "merge and then clean up"? I wrote a script to do this using I also shared some ideas on how the cleanup command could be exposed here:
|
May be useful if you happen to be merging your own PR, though I filed this RFE for the general case that the PR has been merged by someone else. |
@DanyC97 that is the first (manual) step in #380 (comment). |
@jglick I really like this RFE. It's something I've been really wanting to see from a CLI tool for a while. I have a similar script to @francois: git branch --merged origin/master | egrep -v "(?:^\*)|(?:master$)" | xargs git branch -D but unfortunately, the main repo I work on has mandatory squash rules set. I'm interested in your idea of checking if the branches are the same before deleting because a simple Question: how to compare local vs merged PR branchesWhat are your ideas for checking to see if the merged and local PR branches are equivalent? We could do something like What would be cleaner is if GitHub keeps internal track of the "HEAD before the squash/rebase" which could then be used to check against the local PR branch like so: # where $HASH is the HEAD of the remote PR branch before the squash/rebase/merge
git branch --merged $HASH ...and see if the local PR branch is considered "merged" to that Hmmm...thoughts? 🤔 Suggestion: don't update the local base branch ref
I feel like updating the users local base branch ( |
If you are using squash or rebase, I am not sure how it could be done reliably. You are basically going behind Git’s back.
You mean if you are using a non-PR workflow for some changes. In that case updating the base branch would not be a fast-forward so would have to be skipped. Anyway, no strong opinion about whether this implicit |
Wouldn't it be possible to just compare |
Yes I suppose that would suffice in the typical case. (Higher rate of false positives than with true merges, where it is fine if the remote head has been updated past your local ref, for example because a maintainer pushed an additional commit prior to merging.) |
With that in mind, I am thinking the logic could look like this:
|
does the cleanup support added to |
@vilmibm only a rather small part of it. The conditions for it to cleanup the branches are rather specific: So I would not say that |
2 cents from me: This would be super useful. I agree with a solution outlined in #380 (comment) by @jack1142: Let's say
Note: Ideally this should be a separate command like |
Just wanted to chime in my +1 for this being super useful and something I would love for the
And from my git aliases (these are fairly old, cobbled together from StackOverflow/etc, might not be the most efficient way to achieve things, but there might be some bits that help solve things here, as I know I have used it for both merged and squash merged):
#!/bin/sh
#
# List all local branches that have been merged into master.
#
# Ref: https://stackoverflow.com/questions/43489303/how-can-i-delete-all-git-branches-which-have-been-squash-and-merge-via-github/56026209#56026209
# TODO: refactor this to be able to pass in the branch name via props and have it 'just work'
git checkout -q master && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base master $branch) && [[ $(git cherry master $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && echo "$branch"; done
#!/bin/sh
#
# List all local branches that have been merged into staging.
#
# Ref: https://stackoverflow.com/questions/43489303/how-can-i-delete-all-git-branches-which-have-been-squash-and-merge-via-github/56026209#56026209
# TODO: refactor this to be able to pass in the branch name via props and have it 'just work'
git checkout -q staging && git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base staging $branch) && [[ $(git cherry staging $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && echo "$branch"; done |
I've thrown together a draft of this in #7322, if anyone wants to try it out (clone the branch and |
For my somewhat simpler needs, I don't even care to worry about matching up my local branch to the code that was actually merged. I would be happy to simply know whether a PR from a branch with the same name as my local branch has been merged (no matter the method by which it was merged), in which case I'll feel 99% confident that I can delete my local. |
This comment has been minimized.
This comment has been minimized.
https://dev.to/seachicken/safely-clean-up-your-local-branches-9i3 👀 Also
can be automated gh api repos/$(gh api -q .login user)/{repo} --method=PATCH -f delete_branch_on_merge=true |
Hi, I made gh poi. It can clean up local branches even in squash-merged PRs and forked repositories, so I think it fulfills the function of If you are interested, please try it. gh ext install seachicken/gh-poi
gh poi Also, if we find it more convenient to merge to gh cli, I can create PR. |
Describe the feature or problem you’d like to solve
After a PR of mine has been merged I currently have to
git checkout master # if not already on base branch git pull git branch -d local-branch
This is a bunch of manual steps which I need to repeat several times on a good day.
Proposed solution
Navigate to the local clone and
ought to take care of deleting the various branches for me. Or even
to clean up branches from any of my PRs which were merged since I last ran this.
Ideally this would fast-forward my local base branch reference without actually needing to check it out, in case I currently had checked out some other (unmerged) branch or had local modifications.
For bonus points, if some rude maintainer used squash or rebase, verify that the base branch really contains all the changes in my local branch before deleting it. Otherwise I need to check this manually if I am being careful. (Genuine merges behave better: if for example I forgot to push some last-minute commits,
git branch -d
will warn me that the branch is not actually merged. This warning is useless for squashed or rebased PRs because it is always printed.)Additional context
N/A
The text was updated successfully, but these errors were encountered: