git 显示提交之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3368590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show diff between commits
提问by ant2009
I am using Git on Ubuntu 10.04(Lucid Lynx).
我在Ubuntu 10.04(Lucid Lynx)上使用 Git 。
I have made some commits to my master.
我已经向我的主人做了一些承诺。
However, I want to get the difference between these commits. All of them are on my master branch.
但是,我想了解这些提交之间的区别。他们都在我的主分支上。
For example:
例如:
commit dj374
made changes
commit y4746
made changes
commit k73ud
made changes
I want to get the difference between k73ud and dj374. However, when I did the following I couldn't see the changes I made in k73ud
.
我想知道 k73ud 和 dj374 之间的区别。但是,当我执行以下操作时,我看不到我在k73ud
.
git diff k73ud..dj374 > master.patch
回答by VonC
Try
尝试
git diff k73ud^..dj374
to make sure to include all changes of k73ud
in the resulting diff.
确保k73ud
在结果差异中包含所有更改。
git diff
compares two endpoints (instead of a commit range).
Since the OP want to see the changes introduced by k73ud
, he/she needs to difference between the first parent commit of k73ud
: k73ud^
(or k73ud^1
or k73ud~
).
git diff
比较两个端点(而不是提交范围)。由于 OP 希望看到由 引入的更改k73ud
,因此他/她需要区分:(或或)的第一个父提交k73ud
k73ud^
之间的差异。k73ud^1
k73ud~
That way, the diff
results will include changes sincek73ud
parent (meaning including changes from k73ud
itself), instead of changes introduced sincek73ud
(up to dj374
).
这样,diff
结果将包括自k73ud
parent以来的更改(意味着包括k73ud
其自身的更改),而不是自k73ud
(最多dj374
)以来引入的更改。
Also you can try:
您也可以尝试:
git diff oldCommit..newCommit
git diff k73ud..dj374
and (1 space, not more):
和(1 个空格,不能更多):
git diff oldCommit newCommit
git diff k73ud dj374
And if you need to get only files names (e.g. to copy hotfix them manually):
如果您只需要获取文件名(例如手动复制修补程序):
git diff k73ud dj374 --name-only
And you can get changes applied to another branch:
您可以将更改应用于另一个分支:
git diff k73ud dj374 > my.patch
git apply my.patch
回答by Alex Yursha
To see the difference between:
要查看之间的区别:
Your working copy and staging area:
您的工作副本和暂存区:
% git diff
Staging area and the latest commit:
暂存区和最新提交:
% git diff --staged
Your working copy and commit 4ac0a6733:
您的工作副本和提交 4ac0a6733:
% git diff 4ac0a6733
Commit 4ac0a6733 and the latest commit:
提交 4ac0a6733 和最新的提交:
% git diff 4ac0a6733 HEAD
Commit 4ac0a6733 and commit 826793951
提交 4ac0a6733 并提交 826793951
% git diff 4ac0a6733 826793951
For more explanation see the official documentation.
更多解释参见官方文档。
回答by cxreg
If you want to see the changes introduced with each commit, try "git log -p"
如果您想查看每次提交引入的更改,请尝试“git log -p”
回答by geekbytes0xff
gitk --all
- Select the first commit
- Right clickon the other, then diff selected → this
gitk --all
- 选择第一个提交
- 右键单击另一个,然后选择差异→这个
回答by user2647616
I use gitk
to see the difference:
我用gitk
来看区别:
gitk k73ud..dj374
It has a GUI mode so that reviewing is easier.
它有一个 GUI 模式,因此更容易。
回答by AldaronLau
To see the difference between two different commits (let's call them a
and b
), use
要查看两个不同提交之间的区别(让我们称它们为a
和b
),请使用
git diff a..b
- Note that the difference between
a
andb
is opposite fromb
anda
.
- 注意,之间的差
a
并且b
是从相对的b
和a
。
To see the difference between your last commit and not yet committed changes, use
要查看上次提交和尚未提交的更改之间的差异,请使用
git diff
If you want to be able to come back to the difference later, you can save it in a file.
如果您希望稍后能够返回差异,您可以将其保存在一个文件中。
git diff a..b > ../project.diff
回答by Flowkap
Simplest for checking the changes in the last 2 commits after pull:
最简单的检查拉取后最后 2 次提交中的更改:
git diff HEAD~2
回答by Jacob Abraham
I wrote a script which displays diff between two commits, works well on Ubuntu.
我写了一个脚本来显示两次提交之间的差异,在 Ubuntu 上运行良好。
https://gist.github.com/jacobabrahamb4/a60624d6274ece7a0bd2d141b53407bc
https://gist.github.com/jacobabrahamb4/a60624d6274ece7a0bd2d141b53407bc
#!/usr/bin/env python
import sys, subprocess, os
TOOLS = ['bcompare', 'meld']
def execute(command):
return subprocess.check_output(command)
def getTool():
for tool in TOOLS:
try:
out = execute(['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 = execute(['git', '-C', name, 'log', '--oneline', '--reverse']).splitlines()
if first_index >= 0:
commit1 = logs[first_index].split(' ')[0]
if second_index >= 0:
commit2 = logs[second_index].split(' ')[0]
except ValueError:
if first is not '0':
commit1 = first
if second is not '0':
commit2 = second
return commit1, commit2
def validateCommitIds(name, commit1, commit2):
if not commit1 and not commit2:
print "Nothing to do, exit!"
return False
try:
if commit1:
execute(['git', '-C', name, 'cat-file', '-t', commit1])
if commit2:
execute(['git', '-C', name, 'cat-file', '-t', commit2])
except subprocess.CalledProcessError:
return False
return True
def cleanup(commit1, commit2):
execute(['rm', '-rf', '/tmp/'+(commit1 if commit1 else '0'), '/tmp/'+(commit2 if commit2 else '0')])
def checkoutCommit(name, commit):
if commit:
execute(['git', 'clone', name, '/tmp/'+commit])
execute(['git', '-C', '/tmp/'+commit, 'checkout', commit])
else:
execute(['mkdir', '/tmp/0'])
def compare(tool, commit1, commit2):
execute([tool, '/tmp/'+(commit1 if commit1 else '0'), '/tmp/'+(commit2 if commit2 else '0')])
if __name__=='__main__':
tool = getTool()
if not tool:
print "No GUI diff tools, install bcompare or meld"
sys.exit(0)
if len(sys.argv) is not 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 validateCommitIds(name, commit1, commit2) is False:
sys.exit(0)
cleanup(commit1, commit2)
try:
checkoutCommit(name, commit1)
checkoutCommit(name, commit2)
compare(tool, commit1, commit2)
except KeyboardInterrupt:
pass
finally:
cleanup(commit1, commit2)
sys.exit(0)
回答by Manohar Reddy Poreddy
Accepted answer is good.
接受的答案很好。
Just putting it again here, so its easy to understand & try in future
只是把它放在这里,所以它很容易理解和将来尝试
git diff c1...c2 > mypatch_1.patch
git diff c1..c2 > mypatch_2.patch
git diff c1^..c2 > mypatch_3.patch
I got the same diff for all the above commands.
对于上述所有命令,我得到了相同的差异。
Above helps in
1. seeing difference of between commit c1 & another commit c2
2. also making a patch file that shows diff and can be used to apply changes to another branch
以上有助于
1. 查看提交 c1 和另一个提交 c2 之间的差异
2. 还制作一个显示差异的补丁文件,可用于将更改应用于另一个分支
If it not showing difference correctly
then c1 & c2 may be taken wrong
so adjust them to a before commit like c1 to c0, or to one after like c2 to c3
如果它没有正确显示差异,
那么 c1 和 c2 可能是错误的,
因此将它们调整为像 c1 到 c0 这样的前提交,或者像 c2 到 c3 那样的后提交
Use gitk
to see the commits SHAs, 1st 8 characters are enough to use them as c0, c1, c2 or c3. You can also see the commits ids from Gitlab > Repository > Commits, etc.
用gitk
看提交的SHA,1日为8个字符足以将它们作为C0,C1,C2或C3。您还可以从 Gitlab > Repository > Commits 等查看提交 ID。
Hope that helps.
希望有帮助。
回答by bit_cracker007
Let's say you have one more commit at the bottom (oldest), then this becomes pretty easy:
假设您在底部(最旧)还有一个提交,那么这变得非常简单:
commit dj374
made changes
commit y4746
made changes
commit k73ud
made changes
commit oldestCommit
made changes
Now, using below will easily server the purpose.
现在,使用下面将轻松达到目的。
git diff k73ud oldestCommit