如何让git diff只显示两次提交之间的差异,而不包括中间的其他提交?
当前回答
美元git日志
commit-1(new/latest/recent commit)
commit-2
commit-3
commit-4
*
*
commit-n(first commit)
$git diff commit-2
显示commit-2到commit-1之间的所有变化(仅仅是commit-1的补丁,相当于git diff HEAD~1 HEAD)
类似的 $git diff commit-4 commit-1
显示commit-4到commit-1之间的所有更改(commit-1的补丁, 一起提交2号和3号。相当于git差头~3头)
$git diff commit-1 commit-2
通过改变订单提交ID,可以获得恢复补丁。 ("$git diff commit-1 commit-2 > revert_patch_of_commit-1.diff")
其他回答
我写了一个在两次提交之间显示差异的脚本,在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)
假设您想查看提交012345和abcdef之间的差异。以下内容可以达到你的目的:
$ git checkout 012345 $ git cherry-pick -n abcdef $ git diff --cached
我的别名设置在~/。ZSHRC文件git diff:
alias gdf='git diff HEAD{'^',}' # diff between your recent tow commits
谢谢 @Jinmiao罗
git差别头~2头
在最近的第二次提交和当前之间完成更改。
HEAD方便
你可以简单地将2个提交传递给git diff,就像这样:
-> git diff 0da94be 59ff30c > my.patch
-> git apply my.patch
那么这个呢:
git diff abcdef 123456 | less
如果你想要在运行中比较许多不同的差异,将它管道到less是很方便的。
推荐文章
- 如何在Visual Studio中删除未推送的外向提交?
- Git在两个不同的文件之间的差异
- 我如何使用vimdiff来解决git合并冲突?
- 如何将更改提交到另一个预先存在的分支
- 为什么使用'git rm'来删除文件而不是'rm'?
- 我如何安装imagemagick与自制?
- 致命:git-write-tree:错误构建树
- Git克隆远程存储库的特定版本
- git隐藏的意图用例是什么?
- 从远程Git存储库检索特定的提交
- 如何配置git bash命令行补全?
- 我如何迫使git拉覆盖每一个拉上的一切?
- 撤销“git add <dir>”?
- 是否可以在不先签出整个存储库的情况下进行稀疏签出?
- 如何移除SSH密钥?