You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the feature or problem you’d like to solve
After a PR of mine has been merged I currently have to
click the button in the GUI to delete the branch (typically from a fork)
navigate to my local clone
run:
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
gh pr cleanup 123
ought to take care of deleting the various branches for me. Or even
gh pr cleanup --all
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.)
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.
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.
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 git branch -d ... would fail in my scenario every time.
Question: how to compare local vs merged PR branches
What are your ideas for checking to see if the merged and local PR branches are equivalent? We could do something like git diff local-pr..remote/merge-target and making sure the diff is empty. There are definitely issues with just doing a simple git diff ... though. If the local PR branch was behind the remote PR branch and the PR was squashed or rebased, how would we reconcile that? A diff won't work because there will be a variance (unless all the commits were empty).
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 $HASH.
Hmmm...thoughts? 🤔
Suggestion: don't update the local base branch ref
Ideally this would fast-forward my local base branch reference without actually needing to check it out
I feel like updating the users local base branch (master) is too unexpected. I would be surprised if I typed gh pr cleanup --all and come to find my base branch was updated by a cleanup command. Not to mention, what would happen if I had commits on my base branch that aren't in the remote yet?
What are your ideas for checking to see if the merged and local PR branches are equivalent?
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.
what would happen if I had commits on my base branch that aren't in the remote yet?
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 git pull would be unexpected; if not included in the tool, I would always do it anyway.
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.
Wouldn't it be possible to just compare refs/pull/NUMBER/head with local branch?
If the merge (or squash/rebase) was done on the PR through GH that should work, I don't think we can do much about manual squash merges (done locally and pushed by maintainer without using GH to do it)
2 cents from me: This would be super useful. I agree with a solution outlined in #380 (comment) by @jack1142:
Let's say
jakub/my-awesome-branch locally is abcdef1234
jakub/my-awesome-branch was PR 101 on GitHub
PR 101 got squash-merged; at the time of the squash, the HEAD of the branch was abcdef1234
Great, the shas match -> delete the local branch
If shas don't match -> don't delete the local branch, unless --force to delete the branch.
Note: Ideally this should be a separate command like gh branches clean, to clean after things merged 1) via UI, 2) by someone else, 3) via a solution like merge queue.
Just wanted to chime in my +1 for this being super useful and something I would love for the gh CLI to include; and to also contribute a few alternative projects that might be useful in the meantime:
Command-line tool to delete merged Git branches (including squash merged)
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):
# Force delete {local,remote} branch
force-delete-local-branch = branch -D
# Ref: https://stackoverflow.com/questions/39220870/in-git-list-names-of-branches-with-unpushed-commits/48180899#48180899
list-unpushed = list-unpushed-to-origin
list-unpushed-to-origin = log --branches --not --remotes=origin --no-walk --decorate --oneline
# List {local,remote} branches that have been merged into BRANCH
list-local-merged = branch --merged
list-remote-merged = branch --remote --merged
# (Ref: https://stackoverflow.com/questions/43489303/how-can-i-delete-all-git-branches-which-have-been-squash-and-merge-via-github/56026209#56026209)
# List {local,remote} branches that have been merged into {master,staging}
list-local-merged-master = !git list-local-merged master | grep -v master | grep -v staging
list-local-merged-staging = !git list-local-merged staging | grep -v master | grep -v staging
list-remote-merged-master = !git list-remote-merged master | grep -v master | grep -v staging
list-remote-merged-staging = !git list-remote-merged staging | grep -v master | grep -v staging
# List local branches that have been squash merged into {master,staging}
list-local-squash-merged-master = !$ZSH/bin/git-list-local-squash-merged-master
list-local-squash-merged-staging = !$ZSH/bin/git-list-local-squash-merged-staging
# Count {local,remote} branches that have been merged into {master,staging}
count-local-merged-master = !git list-local-merged-master | wc -l
count-local-merged-staging = !git list-local-merged-staging | wc -l
count-remote-merged-master = !git list-remote-merged-master | wc -l
count-remote-merged-staging = !git list-remote-merged-staging | wc -l
# Count local branches that have been squash merged into {master,staging}
count-local-squash-merged-master = !$ZSH/bin/git-list-local-squash-merged-master | wc -l
count-local-squash-merged-staging = !$ZSH/bin/git-list-local-squash-merged-staging | wc -l
# Cleanup {local,remote} merged branches
cleanup-local-merged-master = !git list-local-merged-master | xargs echo RUN THE FOLLOWING IF YOU ARE CERTAIN: git delete-local-branch ;:
cleanup-local-merged-staging = !git list-local-merged-staging | xargs echo RUN THE FOLLOWING IF YOU ARE CERTAIN: git delete-local-branch ;:
cleanup-remote-merged-master = !git list-remote-merged-master | xargs echo RUN THE FOLLOWING IF YOU ARE CERTAIN: git delete-remote-origin-branch ;:
cleanup-remote-merged-staging = !git list-remote-merged-staging | xargs echo RUN THE FOLLOWING IF YOU ARE CERTAIN: git delete-remote-origin-branch ;:
# Cleanup local branches that have been squash merged into {master,staging}
cleanup-local-squash-merged-master = !git list-local-squash-merged-master | xargs echo RUN THE FOLLOWING IF YOU ARE CERTAIN: git force-delete-local-branch ;:
cleanup-local-squash-merged-staging = !git list-local-squash-merged-staging | xargs echo RUN THE FOLLOWING IF YOU ARE CERTAIN: git force-delete-local-branch ;:
git-list-local-squash-merged-master:
#!/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)"|whileread 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
git-list-local-squash-merged-staging:
#!/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)"|whileread 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 make). If anyone has the bandwidth, it would be great to get some help with tests.
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.
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 gh pr cleanup --all.
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.
Reacted by Salim B, Jesse Glick, Lucas Garron, Bas van den Wollenberg, Matias Szylkowski, Shaun Jackman, Brian Gianforcaro, ZeWaka, Marc Philipp, Glenn 'devalias' Grant and 8 more
Activity
billygriffin commentedon Feb 12, 2020
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.
Jackenmen commentedon Feb 13, 2020
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.
francois commentedon Feb 17, 2020
I do the same thing as well, using this script:
It would be very nice to automate this!
Jackenmen commentedon Feb 17, 2020
@francois I don't think that works with squash merging though, right?
francois commentedon Feb 17, 2020
OliverJAsh commentedon Mar 3, 2020
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
hub: https://gist.github.com/OliverJAsh/929c761c8ecbf14d0010634a3f015740#file-git-merge-prI also shared some ideas on how the cleanup command could be exposed here:
mislav/hub#1483 (comment)
jglick commentedon Mar 3, 2020
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 commentedon Apr 29, 2020
@jglick while this is a good RFE wouldn't this help?
jglick commentedon Apr 29, 2020
@DanyC97 that is the first (manual) step in #380 (comment).
eXamadeus commentedon May 7, 2020
@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:
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
git branch -d ...would fail in my scenario every time.Question: how to compare local vs merged PR branches
What are your ideas for checking to see if the merged and local PR branches are equivalent? We could do something like
git diff local-pr..remote/merge-targetand making sure the diff is empty. There are definitely issues with just doing a simplegit diff ...though. If the local PR branch was behind the remote PR branch and the PR was squashed or rebased, how would we reconcile that? A diff won't work because there will be a variance (unless all the commits were empty).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:
...and see if the local PR branch is considered "merged" to that
$HASH.Hmmm...thoughts? 🤔
Suggestion: don't update the local base branch ref
I feel like updating the users local base branch (
master) is too unexpected. I would be surprised if I typedgh pr cleanup --alland come to find my base branch was updated by a cleanup command. Not to mention, what would happen if I had commits on my base branch that aren't in the remote yet?jglick commentedon May 8, 2020
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
git pullwould be unexpected; if not included in the tool, I would always do it anyway.Jackenmen commentedon May 8, 2020
Wouldn't it be possible to just compare
refs/pull/NUMBER/headwith local branch?If the merge (or squash/rebase) was done on the PR through GH that should work, I don't think we can do much about manual squash merges (done locally and pushed by maintainer without using GH to do it)
4 remaining items
jglick commentedon Feb 1, 2021
Even for the special case that I am merging my own PR,
gh pr merge -ddoes not work well currently due to #1444 & #2860.jakub-g commentedon Nov 2, 2022
2 cents from me: This would be super useful. I agree with a solution outlined in #380 (comment) by @jack1142:
Let's say
jakub/my-awesome-branchlocally isabcdef1234jakub/my-awesome-branchwasPR 101on GitHubPR 101got squash-merged; at the time of the squash, the HEAD of the branch wasabcdef1234--forceto delete the branch.Note: Ideally this should be a separate command like
gh branches clean, to clean after things merged 1) via UI, 2) by someone else, 3) via a solution like merge queue.0xdevalias commentedon Dec 7, 2022
Just wanted to chime in my +1 for this being super useful and something I would love for the
ghCLI to include; and to also contribute a few alternative projects that might be useful in the meantime: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):
git-list-local-squash-merged-master:git-list-local-squash-merged-staging:elldritch commentedon Apr 17, 2023
I've thrown together a draft of this in #7322, if anyone wants to try it out (clone the branch and
make). If anyone has the bandwidth, it would be great to get some help with tests.Scotchester commentedon Jul 10, 2024
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.
Awan27091987 commentedon Jul 10, 2024
jglick commentedon Sep 20, 2024
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=trueseachicken commentedon Sep 21, 2024
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
gh pr cleanup --all.If you are interested, please try it.
Also, if we find it more convenient to merge to gh cli, I can create PR.