判断Git提交是否为合并/还原提交 [英] Telling if a Git commit is a Merge/Revert commit

查看:160
本文介绍了判断Git提交是否为合并/还原提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,该脚本需要检查特定的提交是否为合并/还原"提交,并且我想知道是否有git技巧.

I am writing a script that requires checking whether a particular commit is a Merge/Revert commit or not, and I am wondering if there is a git trick for that.

到目前为止,我想出了什么(并且我绝对不想依赖此处的提交消息)是检查HASH^2,看看是否没有错误,是否有更好的方法呢? /p>

What I came up with so far (and I definitely don't want to depend on the commit message here) is to check HASH^2 and see if I don't get an error, is there a better way?

推荐答案

弄清楚某些内容是否为合并很容易.所有这些都包含不止一个父母.要进行检查,您可以这样做,例如

Figuring out if something is a merge is easy. That's all commits with more than one parent. To check for that, you can do, for example

$ git cat-file -p $commit_id

如果输出中有多个父"行,则您发现了合并.

If there's more than one `parent' line in the output, you found a merge.

要还原,就不那么容易了.通常,还原只是正常的提交,恰好是反向应用上一个提交的差异,从而有效地删除了引入的更改.否则没有特别之处.

For reverts it's not as easy. Generally reverts are just normal commits that happen to apply the diff of a previous commit in reverse, effectively removing the changes that commit introduced. There're not special otherwise.

如果使用git revert $commit创建了还原,则git通常会生成一条提交消息,指示该还原及其还原的提交.但是,很有可能以其他方式进行还原,或者仅更改git revert生成的提交的提交消息.

If a revert was created with git revert $commit, then git usually generates a commit message indication the revert and what commit it reverted. However, it's quite possible to do reverts in other ways, or to just change the commit message of a commit generated by git revert.

对于那些您想要实现的目标,寻找那些生成的还原提交消息可能已经足够好了.如果不是这样,您就必须仔细查看其他提交,将它们之间的差异进行比较,查看其中的一个是对另一个提交的完全相反的操作.但是,即使那样也不是一个好的解决方案.通常,足够多的还原与它们还原的提交稍有不同,例如,为了适应在提交和还原之间发生的代码更改.

Looking for those generated revert commit message might already be a good enough heuristic for what you're trying to achieve. If not, you'd have to actually look through other commits, comparing their diffs against each other, looking of one is the exact reverse operation of another. But even that isn't a good solution. Often enough reverts are slightly different than just the reverse of the commit they're reverting, for example to accomodate for code changes that happened between the commit and the revert.

这篇关于判断Git提交是否为合并/还原提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆