简体   繁体   English

删除所有被压扁并合并的本地分支

[英]Delete all local branches that have been squashed and merged

I am working on a fork of a repository.我正在开发一个存储库的分支。 I send pull requests from branches of that fork and they get "Squash and Merged" into the master branch of the upstream repository once they are accepted.我从该分支的分支发送拉取请求,一旦它们被接受,它们就会将“压缩并合并”到上游存储库的主分支中。 How can I automatically find and delete local branches that have already been squashed and merged?如何自动查找和删除已经被压缩和合并的本地分支? Most strategies shown in other solutions rely on determining whether all commits in a branch can be found in master's commit history, but since all my commits are squashed this condition is never met. 其他解决方案中显示的大多数策略都依赖于确定是否可以在 master 的提交历史中找到分支中的所有提交,但是由于我的所有提交都被压缩,所以这个条件永远不会满足。

More Info更多信息

My git remote -v looks like:我的git remote -v看起来像:

origin  git@github.com:sshleifer/transformers_fork.git (fetch)
origin  git@github.com:sshleifer/transformers_fork.git (push)
upstream    git@github.com:huggingface/transformers.git (fetch)
upstream    git@github.com:huggingface/transformers.git (push)

Through git alone: the short answer is you can't (not with 100% reliability).通过 git 单独:简短的回答是你不能(不是 100% 的可靠性)。

Here are some unreliable ways to explore:以下是一些不可靠的探索方式:

  • if the commit messages on master contain the branch name: grep it out;如果 master 上的提交消息包含分支名称: grep 输出;
  • if the commit messages on master contain a PR name: grep it out, then use Github's api to find what branch is linked to this PR;如果 master 上的提交消息包含 PR 名称:grep 出来,然后使用 Github 的 api 查找与此 PR 链接的分支;
  • if the commit messages on master contain an issue number: grep it out, and match it with your own rule (issue number in branch name? fix #xyz in some commit message?);如果 master 上的提交消息包含问题编号:grep 将其取出,并将其与您自己的规则匹配(分支名称中的问题编号?在某些提交消息中fix #xyz ?);
  • if the branches are always merged soon enough, or rebased on master before being squashed + merged, you can try to look if the content (the ^{tree} ) in your branch matches the content of a commit on master:如果分支总是很快合并,或者在被压扁+合并之前重新基于 master,您可以尝试查看分支中的内容^{tree} )是否与 master 上的提交内容匹配:
    • git log --first-parent --format="%T" will give you the list of trees on master, git log --first-parent --format="%T"会给你master上的树列表,
    • git rev-parse branch/name^{tree} will give you the tree for branch branch/name git rev-parse branch/name^{tree}将为您提供分支branch/name的树

If you can persuade the owners of the upstream repository, you could use a workflow where instead of generating pull requests from the fork to some long-lived branch of the upstream, you instead generate pull requests against a short-lived feature branch that has the exact same name as the branch you are pushing, and then separately the administrator can manage a pull request from that feature branch (in the upstream) to the long-lived branch.如果您可以说服上游存储库的所有者,您可以使用工作流,而不是从分支生成拉取请求到上游的某个长期存在的分支,而是针对具有短期特性的分支生成拉取请求与您正在推送的分支完全相同的名称,然后管理员可以单独管理从该功能分支(在上游)到长期分支的拉取请求。 If this deletes the feature branch upon the squash merge, then you could detect when upstream feature branches are deleted with remote prune .如果这会在 squash 合并时删除功能分支,那么您可以检测到上游功能分支何时被remote prune删除。

# My PR to upstream creates a new feature branch
fork:my-feature-branch -> upstream:my-feature-branch

# Upstream deals with merging to long-lived branch
# this operation will delete upstream:my-feature-branch
upstream:my-feature-branch -> upstream:develop

# I synchronize my fork with upstream and observe branches removed
# with remote prune, and either manually or programmatically delete
# the fork: version of those branches.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM