Show diff between commits

只愿长相守 提交于 2019-12-17 17:19:49

问题


I am using Git on Ubuntu 10.04 (Lucid Lynx).

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.

git diff k73ud..dj374 > master.patch

回答1:


Try

git diff k73ud^..dj374

to make sure to include all changes of k73ud in the resulting diff.

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~).

That way, the diff results will include changes since k73ud parent (meaning including changes from k73ud itself), instead of changes introduced since k73ud (up to dj374).

Also you can try:

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

and (1 space, not more):

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



回答2:


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:

% git diff 4ac0a6733

Commit 4ac0a6733 and the latest commit:

% git diff 4ac0a6733 HEAD

Commit 4ac0a6733 and commit 826793951

% git diff 4ac0a6733 826793951

For more explanation see the official documentation.




回答3:


If you want to see the changes introduced with each commit, try "git log -p"




回答4:


I use gitk to see the difference:

gitk k73ud..dj374

It has a GUI mode so that reviewing is easier.




回答5:


  1. gitk --all
  2. Select the first commit
  3. Right click on the other, then diff selected → this



回答6:


To see the difference between two different commits (let's call them a and b), use

git diff a..b
  • Note that the difference between a and b is opposite from b and 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



回答7:


Simplest for checking the changes in the last 2 commits after pull:

git diff HEAD~2 



回答8:


I wrote a script which displays diff between two commits, works well on 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)



回答9:


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



回答10:


Use this command for the difference between commit and unstaged:

git difftool --dir-diff


来源:https://stackoverflow.com/questions/3368590/show-diff-between-commits

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!