简体   繁体   English

如何删除所有本地分支,如果合并为主分支将导致无变化?

[英]How can I delete all local branches which would result in no changes if merged into master?

I know how to delete all local branches that have been merged . 我知道如何删除已合并的所有本地分支 However, and I believe this is due to Github's new pull request squash and merge feature, I find myself being left with a lot of local branches that are unmerged, but if merged into master would result in no changes. 但是,我相信这是由于Github的新拉动请求压缩和合并功能,我发现自己留下了许多未合并的本地分支,但如果合并到master中将导致没有变化。

How can I prune these local branches, ie those local branches which haven't necessarily been merged, but wouldn't affect master (or, more generically, the current branch)? 如何修剪这些本地分支,即那些未必合并但不会影响主分支(或更一般地说,当前分支)的本地分支?

There is no perfect solution but you can get close, perhaps close enough. 没有完美的解决方案,但你可以接近,也许足够接近。

Be sure to start with a clean work tree and index (see require_clean_work_tree in git-sh-setup ). 一定require_clean_work_tree干净的工作树和索引开始(请参阅git-sh-setup require_clean_work_tree )。

For each candidate branch $branch that might be delete-able: 对于可能可删除的每个候选分支$branch

  1. Find its merge target (presumably merge_target=$(git config --get branch.${branch}.merge) ). 找到它的合并目标(大概是merge_target=$(git config --get branch.${branch}.merge) )。 Check out the merge target. 查看合并目标。
  2. Do a merge with --no-commit ; --no-commit合并; or in step 1, check out with --detach so that you will get a commit you can abandon, if the merge succeeds. 或者在步骤1中,使用--detach检查,以便在合并成功时获得可以放弃的提交。
  3. Test whether git thinks the merge succeeded, and if so, whether the current tree matches the previous tree, ie, brought in no changes. 测试git是否认为合并成功,如果是,则当前树是否与前一棵树匹配,即没有带来任何变化。 If you can test exact matches, and if you allow the commit to happen (via --detach ), you can do this last test very simply, without any diff-ing: run both git rev-parse HEAD^{tree} and git rev-parse HEAD^^{tree} 1 and see if they produce the same hash. 如果你可以测试完全匹配,并且你允许提交(通过--detach ),你可以非常简单地完成最后一次测试,没有任何差异:运行git rev-parse HEAD^{tree}git rev-parse HEAD^^{tree} 1并查看它们是否产生相同的哈希值。 If you don't allow the commit, you can still git diff the current ( HEAD ) commit against the proposed merge. 如果不允许犯,你仍然可以git diff的电流( HEAD )提交反对提出的合并。 If you need to remove some noise from the diff (eg, config files that should not be, but are anyway, in the commits), this gives you a place to do it. 如果你需要从diff中删除一些噪音(例如,配置文件不应该,但无论如何,在提交中),这给你一个地方去做。
  4. Reset ( git merge --abort; git reset --hard HEAD; git clean -f or similar, depending on how you have decided to implement steps 1-3). 重置( git merge --abort; git reset --hard HEAD; git clean -f或类似,具体取决于你决定如何实现步骤1-3)。 This is just meant to make your work tree and index clean again, for the next pass. 这只是为了让您的工作树和索引再次清理,以便下一次传递。
  5. If the merge in step 3 worked and introduced no changes, you may delete the local branch. 如果步骤3中的合并有效并且未引入任何更改,则可以删除本地分支。 Otherwise, keep it. 否则,保持它。

In essence, this is "actually do the merge and see what happens", just fully automated. 实质上,这是“实际上合并,看看会发生什么”,只是完全自动化。


1 This notation looks a bit bizarre, but it's just HEAD^ —the first parent of HEAD —followed by ^{tree} . 1这个符号看起来有点怪异,但它只是HEAD^的-the第一个父HEAD通过-followed ^{tree} Alternate spellings might be easier to read: HEAD~1^{tree} or ${merge_target}^tree , where ${merge_target} is the branch you checked out in step 1. Note that this assumes the merge succeeded. 替代拼写可能更容易阅读: HEAD~1^{tree}${merge_target}^tree ,其中${merge_target}是您在步骤1中检出的分支。请注意,这假设合并成功。 The merge result is in the exit status of git merge : zero means succeeded, nonzero means failed and needs manual assistance, presumably due to a merge conflict. 合并结果处于git merge的退出状态:零表示成功,非零表示失败,需要手动帮助,可能是由于合并冲突。

If you run git "branch -v" the tracking branches which have changes will have "ahead" written next to them. 如果你运行git“branch -v”,那些有变化的跟踪分支将在它们旁边写下“前面”。

The other two options: "behind" and if nothing is written, means that the branches have no changes that will affect the branches they track. 另外两个选项:“后面”并且如果没有写入,则意味着分支没有会影响它们跟踪的分支的更改。

You can therefore run "git fetch" to update the remote tracking branches then parse the "git branch -v" results to figure out which branches have no changes and which branches have. 因此,你可以运行“git fetch”来更新远程跟踪分支,然后解析“git branch -v”结果,找出哪些分支没有变化,哪些分支有。

暂无
暂无

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

相关问题 Git:如何删除所有本地分支(合并与否),除了master和develop - Git : How to delete all local branches (merged or not), except master and develop 如果看起来不错,我如何列出合并到master的git分支并删除这些分支? - How can i list the git branches which are merged to master and delete the branches if it looks good? 如何清除(删除)所有合并的本地分支? - How to clear (delete) all merged local branches? 如何将本地 master 合并到所有本地分支? - How can I merge local master into all local branches? 如何使用 PowerShell 删除 Git 中所有合并的本地分支 - How to delete all merged local branches in Git with PowerShell 如何删除所有已合并的 Git 分支? - How do I delete all Git branches which have been merged? 我将分支开发合并到主分支,但在主分支上,开发没有变化 - I merged branches develop into master but on master branch no changes from develop 我可以使用相同的分支来恢复合并到 master 的更改吗 - Can I use same branch to revert changes which is merged to master 仅本地更改-如何删除没有相应本地分支的所有远程跟踪分支? - Local changes only - How do I delete all remote tracking branches without a corresponding local branch? Git:如何列出从特定日期合并到主控的所有远程分支/按日期排序 - Git : how to list all remote branches which ve been merged to master from a specific date / sorted by date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM