我可以看看2个提交补丁是否相同

时间:2017-03-21 08:13:00

标签: git

如果一个分支提交A-B-C而另一个分支提交A-C'-B', 是否有某种方法来制作B和B'的补丁? 我想(脚本a)比较两个分支,并报告差异,所以在上面的情况下我希望它注意到它们是相同的。 如果我在C和B之间使用log'将显示2个提交,即使它们是另一个命令中的相同提交。

经过测试可以在git show之间做一些差异,但由于行号不同,我不知道如何让它们相等。

这是一个设置git测试用例的脚本:

git init
seq 1 50 > a
git add a
git commit -m "fill a"

git checkout -b top_bottom
sed -i '1 a\
Add this line high up in the file' a
git add a
git commit -m "top of a"
sed -i '$ a\
Add this line at the bottom of the file' a
git add a
git commit -m "bottom of a"
git checkout master

git checkout -b bottom_top
git cherry-pick top_bottom
git cherry-pick top_bottom~
git checkout master

进行比较,例如diff <(git show --format=raw top_bottom) <(git show --format=raw bottom_top~1)显示补丁部分中的行数差异(以及标题中预期的散列差异)

2 个答案:

答案 0 :(得分:2)

要比较补丁,我宁愿使用git diff

git diff B^ B > B.patch

收回你的榜样:

# name B1 and B2 the 2 commits you want to inspect :
git tag B1 top_bottom^
git tag B2 bottom_top

获取条款:

# first patch :
git diff B1^ B1 > B1.patch
# second patch :
git diff B2^ B2 > B2.patch

直言不讳的比较仍然会显示行数的差异:

diff B1.patch B2.patch

近似值是:删除提及行号的行,并查看简单的&#34;添加/删除&#34;块匹配:

cat B1.patch |
  grep -v "^index" |  # remove lines which mention blob or tree hashes
  grep -v "^@@"       # remove lines which mention line numbers
    > B1.trimmed

# same with B2

diff B1.trimmed B2.trimmed

答案 1 :(得分:2)

You want git patch-id,专为此目的而设计。

我必须为FreeBSD sed略微修改您的设置脚本(将sed -i '1a\更改为sed -i -e '1a\),但这样做了:

$ git show bottom_top | git patch-id --stable
3a28b42e4aae9c248c00c63451756cbd881cf046 2c33ad0a41076e5a47ec780e8ab6c6b8450db582
$ git show top_bottom~1 | git patch-id --stable
3a28b42e4aae9c248c00c63451756cbd881cf046 7cd871013b7bbfc69f03315d43285a2b78b81a86

第一个数字是补丁ID(您可以使用--stable--unstable;阅读文档以决定使用哪个),第二个是提交哈希。请注意,top_bottom生成不同的修补程序ID(当然,来自不同的提交):

$ git show top_bottom | git patch-id --stable
69febc37e58b8aeb210b3e1e71fa59a11c369a74 9a34efdfe098f6b690199a124cc3ce34f48002b8