我在Ubuntu 10.04 (Lucid Lynx)上使用Git。

我已经向我的主人许下了一些承诺。

但是,我想知道这些提交之间的区别。它们都在我的主分支上。

例如:

commit dj374
made changes

commit y4746
made changes

commit k73ud
made changes

我想知道k73ud和dj374的区别。然而,当我执行以下操作时,我无法看到我在k73ud中所做的更改。

git diff k73ud..dj374 > master.patch

当前回答

接受的答案很好。

只是把它放在这里,所以它很容易理解和将来尝试

git diff c1...c2 > mypatch_1.patch  
git diff c1..c2  > mypatch_2.patch  
git diff c1^..c2 > mypatch_3.patch  

对于上述所有命令,我都得到了相同的差异。

以上有助于 1. 看到commit c1和另一个commit c2之间的差异 2. 还可以制作一个显示diff的补丁文件,并可用于将更改应用到另一个分支

如果它没有正确地显示差异 那么c和c可能会被误解 所以在提交前将它们调整为a,比如c1到c0,或者在提交后将它们调整为1,比如c2到c3

使用gitk查看提交的sha,前8个字符足够使用它们作为c0, c1, c2或c3。你还可以看到Gitlab > Repository > commit的提交id,等等。

希望这能有所帮助。

其他回答

gitk——所有 选择第一次提交 右键点击另一个,然后diff selected→this

我写了一个在两次提交之间显示差异的脚本,在Ubuntu上工作得很好。

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)

假设你在底部(最老的)还有一个提交,那么这就变得很简单了:

commit dj374
made changes

commit y4746
made changes

commit k73ud
made changes

commit oldestCommit
made changes

现在,使用下面的方法将很容易达到目的。

git diff k73ud oldestCommit
 1. git diff <commit-id> <commit-id>
 2. git diff HEAD^ HEAD     -(HEAD = current branch’s tip),( HEAD^ = version before the last commit)
 3. git diff HEAD^ HEAD — ./file     (comparison to specified file)
 4. git diff HEAD~5 HEAD           - (HEAD~5 refers to the last 5 commits.)

使用这个命令来区分commit和unstaging:

git difftool --dir-diff