简体   繁体   English

检查分支是否已合并为主分支并且尚未恢复

[英]Check if a branch has been merged to master and hasn't been reverted out

I would like to create a tool that will list out the names of feature branches that have been merged into a branch (eg master), but excluding branches whose merges have since been reverted. 我想创建一个工具,列出已合并到分支(例如master)的功能分支的名称,但不包括其合并已被还原的分支。

For example: 例如:

   /--*--*--*--\                           feature
  /             \
------*--*-------*--*--*--*-------------*  master
                 ^     ^
                 |     | git revert -m1 abc1 (#abc2)
                 |       
                 | git merge --no-ff feature (#abc1)

I understand that git branch --contains feature will return master , and I understand why, but I would like to know if there is a simple-enough way to exclude branches that have since been reverted out. 我知道git branch --contains feature将返回master ,我理解为什么,但我想知道是否有一种简单的方法来排除已经被恢复的分支。

If not, what would be optimal solution? 如果没有,最佳解决方案是什么? Comparing diffs? 比较差异?

First, from " How can I know in git if a branch has been already merged into master? ", you can check if your feature branch is part of git branch --merged or check git merge-base to find the merged commit. 首先,从“ 如何在git中知道分支是否已经合并到master中? ”,您可以检查您的功能分支是否是git branch --merged一部分git branch --merged或检查git merge-base以查找合并的提交。

Second, the question " Telling if a Git commit is a Merge/Revert commit " does warn us that a revert commit can be hard to find, and only advocate for the revert message boilerplate in the commit message. 其次,问题“ 告诉Git提交是合并/恢复提交 ”确实警告我们,很难找到恢复提交,并且只提倡提交消息中的恢复消息样板。

Do the simple search which is 做简单的搜索

git log --grep="This reverts commit ${merge_sha}"

If they hand edited the commit message then you are stuck with something awful like 如果他们手工编辑了提交消息,那么你就会遇到类似的问题

matchId=$(git diff  ${merge_sha}^! | grep -v -e "^diff --git" -e "^--- " -e "^+++ " -e "^index " | sha1sum

for sha in $(git revlist ${merge_sha}..HEAD)
do
  shaId=$(git diff  -R ${sha}^! | grep -v -e "^diff --git" -e "^--- " -e "^+++ " -e "^index " | sha1sum)
  if [ ${matchId} == ${shaId} ]
  then
      echo "${sha} might be a ninja revert of ${merge_sha}"
   fi
done

This could obviously be improved a lot to reverse the fields of some of the lines that I am excluding with grep currently. 这显然可以大大改善,以扭转我目前用grep排除的某些行的字段。 You'd basically be re-implementing git patch-id but allowing it to have the a/b files all reversed (maybe there is an option to allow that already, but I don't know what it is) 你基本上是在重新实现git patch-id但是允许它将所有的a / b文件都反转(也许有一个选项允许它已经,但我不知道它是什么)

An easy way to do this would be to simply grep through the log: 一个简单的方法是简单地浏览日志:

git log | grep "^commit " | grep <commit-id>
  • git log — shows the log of all the commits git log - 显示所有提交的日志
  • grep "^commit " — filters by commit ID (since they all start with "commit") grep "^commit " - 按提交ID过滤(因为它们都以“commit”开头)
  • grep <commit-id> — filter by just the commit you care about grep <commit-id> - 仅根据您关注的提交进行过滤

NOTE —This may not work if git changes the commit ID. 注意 - 如果git更改提交ID,这可能不起作用。

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

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