【问题标题】:Show diff between commits显示提交之间的差异
【发布时间】:2011-03-23 01:36:57
【问题描述】:

我在 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 git-diff


    【解决方案1】:

    试试

    git diff k73ud^..dj374
    

    确保在结果差异中包含k73ud 的所有更改。

    git diff 比较两个端点 (instead of a commit range)。 由于 OP 想看到k73ud 引入的变化,他/她需要区分first parent commit of k73ud: k73ud^(或k73ud^1 or k73ud~)。

    这样,diff 结果将包括 since k73ud parent 的更改(意味着包括来自k73ud 本身的更改),而不是引入的更改since @ 987654337@(最多dj374)。

    你也可以试试:

    git diff oldCommit..newCommit
    git diff k73ud..dj374 
    

    和(1个空格,不多):

    git diff oldCommit newCommit
    git diff k73ud dj374
    

    如果您只需要获取文件名(例如手动复制修补程序):

    git diff k73ud dj374 --name-only
    

    您可以将更改应用到另一个分支:

    git diff k73ud dj374 > my.patch
    git apply my.patch
    

    【讨论】:

    • 你确定吗? git的差异275e8922ab4e995f47a753b88b75c3027444a54c..a8d9d944c32e945cbb9f60b3f724ecc580da86ae作品,但git的差异275e8922ab4e995f47a753b88b75c3027444a54c ^ .. a8d9d944c32e945cbb9f60b3f724ecc580da86ae得到错误信息 - “未知版本或路径不工作的树” SPAN>
    • @demas:在我的机器上工作;)你也可以使用git diff 275e8^ a8d9d9,因为它与'..'相同。
    • @VonC 在我的机器上,不需要使用^
    • @VonC Ubuntu 14.04。只有git diff k73ud..dj374 可以
    • @BradyDowling 同意。如果您想查看 PR 差异,可以在命令行中使用新的 gh CLI:stackoverflow.com/a/62031065/6309
    【解决方案2】:

    要查看两者之间的区别:

    您的工作副本和暂存区:

    % git diff
    

    暂存区和最新提交:

    % git diff --staged
    

    你的工作副本和提交 4ac0a6733:

    % git diff 4ac0a6733
    

    提交 4ac0a6733 和最新的提交:

    % git diff 4ac0a6733 HEAD
    

    提交 4ac0a6733 并提交 826793951

    % git diff 4ac0a6733 826793951
    

    更多解释见the official documentation

    【讨论】:

    • 另外,如果您真的只是想在这些提交中查看一个文件的差异,git diff {x} {y} -- filename 其中{x}{y} 是给出的任何示例。另请参阅git log -p,因为有一些重叠。
    【解决方案3】:

    如果您想查看每次提交时引入的更改,请尝试“git log -p”

    【讨论】:

    • MVP!现在我怎么能在两个特定的哈希之间做到这一点?并反转(从旧到更新)? git log -p --reverse old_hash..new_hash!
    【解决方案4】:
    1. gitk --all
    2. 选择第一个提交
    3. 右击另一个,然后diff selected → this

    【讨论】:

    • 我对 gitk 的信任度开始降低一点,因为它显示的提交者作者与实际提交者作者不同。
    【解决方案5】:

    要查看两个不同提交之间的区别(我们称它们为 ab),请使用

    git diff a..b
    
    • 请注意ab 之间的区别与ba 是相反的。

    要查看上次提交和尚未提交的更改之间的差异,请使用

    git diff
    

    如果您希望稍后能够恢复差异,可以将其保存在文件中。

    git diff a..b > ../project.diff
    

    【讨论】:

      【解决方案6】:

      我用gitk来看看区别:

      gitk k73ud..dj374
      

      它有一个GUI模式,所以审查更容易。

      【讨论】:

        【解决方案7】:

        在拉取后检查最后 2 次提交中的更改最简单:

        git diff HEAD~2 
        

        【讨论】:

        • 对我来说,这在最后一次提交到倒数第二个(拉后)之间做一个差异:git diff HEAD~1
        【解决方案8】:

        我写了一个脚本,显示两次提交之间的差异,在 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)
        

        【讨论】:

        • 有趣的脚本。 +1
        【解决方案9】:

        我一直喜欢使用命令行,并且手头有用户友好的工具(带有 GUI)。两全其美。这是我在 Git 中比较两个提交的方法。

        您可以显示两个提交之间的差异,如下所示。

        在文本编辑器中编辑您的 git 配置文件:

        git config --global -e 
        

        在 Windows 中的 Git 配置文件中设置一个适当的差异工具(用户友好),例如 Meld:

        [difftool "meld"]
        cmd = "C:/Program Files (x86)/Meld/Meld.exe" "LOCAL\" \"REMOTE" --label "DIFF (ORIGINAL MY)"
        prompt = false
        path = C:\Program Files (x86)\Meld\Meld.exe
        

        Meld 可以像这样从命令行使用 Chocolatey 安装:

        choco install meld
        

        让我们定义一个shell函数来帮助我们比较TEXT EDITOR中[alias]下的两个sha-s(提交):

        [alias]
        showchangesbetween = "!w() { git difftool \"$1\" \"$2\" --dir-diff --ignore-all-space; }; w"
        

        要在 Meld 的帮助下比较提交(或您喜欢的其他差异工具,只需在命令行中输入:

        git showchangesbetween somesha123 somesha456
        

        提交的 sha-s 输入很容易看到

         git log 
        

        例如。

        【讨论】:

          【解决方案10】:

          接受的答案很好。

          在这里再放一次,这样很容易理解和将来尝试

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

          我对上述所有命令都有相同的差异。

          以上帮助
          1. 查看提交 c1 和另一个提交 c2 之间的区别
          2. 还制作了一个显示差异的补丁文件,可用于将更改应用到另一个分支

          如果它没有正确显示差异
          那么 c1 & c2 可能会被取错
          所以将它们调整为之前的提交,例如 c1 到 c0,或之后的提交,例如 c2 到 c3

          使用gitk 查看提交的 SHA,前 8 个字符足以将它们用作 c0、c1、c2 或 c3。您还可以从 Gitlab > Repository > Commits 等中查看提交 id。

          希望对您有所帮助。

          【讨论】:

            【解决方案11】:
             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.)
            

            【讨论】:

              【解决方案12】:

              下面的命令在 Ubuntu 20.04 和 git v2.25.1 上非常适合我:

              git diff <base-commit-id> <target-commit-id>
              

              【讨论】:

                【解决方案13】:

                假设您在底部(最旧的)还有一个提交,那么这变得非常简单:

                commit dj374
                made changes
                
                commit y4746
                made changes
                
                commit k73ud
                made changes
                
                commit oldestCommit
                made changes
                

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

                git diff k73ud oldestCommit
                

                【讨论】:

                  【解决方案14】:

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

                  git difftool --dir-diff
                  

                  【讨论】:

                    猜你喜欢
                    • 2021-09-12
                    • 1970-01-01
                    • 2021-04-28
                    • 1970-01-01
                    • 2012-06-06
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-02-13
                    相关资源
                    最近更新 更多