您如何git diff
只显示两次提交之间的差异,而不包括两者之间的其他提交?
您如何git diff
只显示两次提交之间的差异,而不包括两者之间的其他提交?
Answers:
您可以简单地将2次提交传递给git diff,例如:
-> git diff 0da94be 59ff30c > my.patch
-> git apply my.patch
my.patch
其他分支机构?
要求两次提交之间的差异而不包含两次之间的提交几乎没有道理。提交只是存储库内容的快照;要求两者之间的差异必然包括它们。那么问题来了,您到底在寻找什么?
正如William所建议的那样,挑选樱桃可以为您提供一次基于另一个提交的提交的增量。那是:
$ git checkout 012345
$ git cherry-pick -n abcdef
$ git diff --cached
这将提交'abcdef',将其与其直接祖先进行比较,然后将该差异应用于'012345'之上。然后显示了这一新差异-唯一的变化是上下文来自“ 012345”而不是“ abcdef”的直接祖先。当然,您可能会遇到冲突等,因此在大多数情况下,这不是一个非常有用的过程。
如果您仅对abcdef本身感兴趣,可以执行以下操作:
$ git log -u -1 abcdef
这将abcdef与它的直接祖先进行比较,通常是您想要的。
而且当然
$ git diff 012345..abcdef
给您这两个提交之间的所有差异。
这将有助于更好地了解您要实现的目标-正如我提到的那样,在两次提交之间没有区别的情况下寻求差异实际上是没有意义的。
origin/featurebranch#HEAD
来local/featurebranch#HEAD
可以帮助你确保你没有解决冲突的过程中渣土什么。
要将两个git commit 12345和abcdef作为补丁进行比较,可以使用diff命令作为
diff <(git show 123456) <(git show abcdef)
git diff <(git show 123456) <(git show abcdef)
不起作用;diff <(...) <(...)
做。(我刚刚尝试过)。
git diff 123456 abcdef
。
diff
从两个diff
s中获取输出。这涉及读取和比较两个输入流。 diff
(GNU或Unix diff
)可以做到,而git diff
不能。有些人可能想知道为什么要这样做。我现在正在这样做,正在清理合并失败的合并。
要检查完整的更改:
git diff <commit_Id_1> <commit_Id_2>
仅检查更改/添加/删除的文件:
git diff <commit_Id_1> <commit_Id_2> --name-only
注意:要检查diff而没有提交,则不需要放置提交ID。
假设您要查看提交012345和abcdef之间的区别。以下应做您想要的:
$ git checkout 012345 $ git cherry-pick -n abcdef $ git diff-缓存
从Git 2.19开始,您可以简单地使用:
git range-diff rev1...rev2
-比较两个提交树,从它们的共同祖先开始
或
git range-diff rev1~..rev1 rev2~..rev2
-比较2次给定提交引入的更改
我编写了一个脚本,该脚本显示两次提交之间的差异,在Ubuntu上效果很好。
https://gist.github.com/jacobabrahamb4/a60624d6274ece7a0bd2d141b53407bc
#!/usr/bin/env python
import sys, subprocess, os
TOOLS = ['bcompare', 'meld']
def getTool():
for tool in TOOLS:
try:
out = subprocess.check_output(['which', tool]).strip()
if tool in out:
return tool
except subprocess.CalledProcessError:
pass
return None
def printUsageAndExit():
print 'Usage: python bdiff.py <project> <commit_one> <commit_two>'
print 'Example: python bdiff.py <project> 0 1'
print 'Example: python bdiff.py <project> fhejk7fe d78ewg9we'
print 'Example: python bdiff.py <project> 0 d78ewg9we'
sys.exit(0)
def getCommitIds(name, first, second):
commit1 = None
commit2 = None
try:
first_index = int(first) - 1
second_index = int(second) - 1
if int(first) < 0 or int(second) < 0:
print "Cannot handle negative values: "
sys.exit(0)
logs = subprocess.check_output(['git', '-C', name, 'log', '--oneline', '--reverse']).split('\n')
if first_index >= 0:
commit1 = logs[first_index].split(' ')[0]
if second_index >= 0:
commit2 = logs[second_index].split(' ')[0]
except ValueError:
if first != '0':
commit1 = first
if second != '0':
commit2 = second
return commit1, commit2
def validateCommitIds(name, commit1, commit2):
if commit1 == None and commit2 == None:
print "Nothing to do, exit!"
return False
try:
if commit1 != None:
subprocess.check_output(['git', '-C', name, 'cat-file', '-t', commit1]).strip()
if commit2 != None:
subprocess.check_output(['git', '-C', name, 'cat-file', '-t', commit2]).strip()
except subprocess.CalledProcessError:
return False
return True
def cleanup(commit1, commit2):
subprocess.check_output(['rm', '-rf', '/tmp/'+(commit1 if commit1 != None else '0'), '/tmp/'+(commit2 if commit2 != None else '0')])
def checkoutCommit(name, commit):
if commit != None:
subprocess.check_output(['git', 'clone', name, '/tmp/'+commit])
subprocess.check_output(['git', '-C', '/tmp/'+commit, 'checkout', commit])
else:
subprocess.check_output(['mkdir', '/tmp/0'])
def compare(tool, commit1, commit2):
subprocess.check_output([tool, '/tmp/'+(commit1 if commit1 != None else '0'), '/tmp/'+(commit2 if commit2 != None else '0')])
if __name__=='__main__':
tool = getTool()
if tool == None:
print "No GUI diff tools"
sys.exit(0)
if len(sys.argv) != 4:
printUsageAndExit()
name, first, second = None, 0, 0
try:
name, first, second = sys.argv[1], sys.argv[2], sys.argv[3]
except IndexError:
printUsageAndExit()
commit1, commit2 = getCommitIds(name, first, second)
if not validateCommitIds(name, commit1, commit2):
sys.exit(0)
cleanup(commit1, commit2)
checkoutCommit(name, commit1)
checkoutCommit(name, commit2)
try:
compare(tool, commit1, commit2)
except KeyboardInterrupt:
pass
finally:
cleanup(commit1, commit2)
sys.exit(0)