450

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ant2009
  • 27,094
  • 154
  • 411
  • 609

15 Answers15

517

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 wants to see the changes introduced by k73ud, they need to differentiate 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
verwirrt
  • 125
  • 1
  • 9
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 6
    Are you sure? git diff 275e8922ab4e995f47a753b88b75c3027444a54c..a8d9d944c32e945cbb9f60b3f724ecc580da86ae works, but git diff 275e8922ab4e995f47a753b88b75c3027444a54c^..a8d9d944c32e945cbb9f60b3f724ecc580da86ae get error message - "unknown revision or path not in the working tree" – ceth Jul 30 '10 at 06:03
  • 1
    @demas: works on my machine ;) you can also use `git diff 275e8^ a8d9d9` since it is the same then '`..`'. – VonC Jul 30 '10 at 06:48
  • 4
    @VonC In my machine, there is no need to use ^ – xi.lin Aug 11 '14 at 07:56
  • @xi.lin what OS are you using? On Windows shell (cmd), you would need `^^` – VonC Aug 11 '14 at 07:57
  • 5
    @VonC Ubuntu 14.04. Only `git diff k73ud..dj374` is OK – xi.lin Aug 11 '14 at 07:59
  • @xi.lin I agree. I guess the branch topology must have been a special case for the OP to have to use `k73ud^`. – VonC Aug 11 '14 at 08:03
  • @Eug Are you sure about your edit `git diff dj374 k73ud`? `k73ud` is supposed to be the old commit, not the new one. – VonC Jan 18 '19 at 11:08
  • @Eug Thank you for your edit. Note that it does exclude changes from `k73ud` (the old commit) itself, which might not be what the OP wanted – VonC Jan 18 '19 at 14:59
  • @demas Is 275e892 the first commit in your repo? If so, it has no parent, so you wouldn't use `^` after it or `^^` on Windows Command Prompt (and using the caret would give that error). Doing so in that case would be the equivalent of `diff -r /dev/null /path/to/repo` (which is just every line with a `+` before it, or in other words, is nonsensical). – Poikilos Jun 29 '19 at 12:42
  • If your repository is on Github and you want to see the diff in a GUI then see this guide https://help.github.com/en/github/committing-changes-to-your-project/comparing-commits#comparing-commits – Brady Dowling May 28 '20 at 19:47
  • 1
    @BradyDowling Agreed. And if you want to see a PR diff, you can do so in command line with the new `gh` CLI: https://stackoverflow.com/a/62031065/6309 – VonC May 28 '20 at 19:50
  • Re: "[_it does exclude changes from k73ud (the old commit)_](https://stackoverflow.com/questions/3368590/show-diff-between-commits#comment95336946_3368758)", I think the odd thing here is that the OP has the **newest commit** from his chronological list of changes **first in his `git diff` call**. That's fine; `git diff` just compares commit states & doesn't care about age, but it _is_ making this answer harder for me to wrap my head around. That is, I think the answer is still a step away from translating `git diff oldCommit newCommit` to "when & how `^` helps you diff _any_ two commits". 2¢ – ruffin Feb 20 '23 at 15:32
177

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Yursha
  • 3,208
  • 3
  • 26
  • 25
  • 15
    also, if you really just want to see one file's diffs in those commits, `git diff {x} {y} -- filename` where `{x}` and `{y}` are any of those examples given. See also, `git log -p`, since there's some overlap. – michael Sep 24 '17 at 22:11
71

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

cxreg
  • 11,088
  • 1
  • 20
  • 10
  • 2
    MVP! Now how can I do that between two specific hashes? And reversed (from older to more recent)? `git log -p --reverse old_hash..new_hash`! – David 天宇 Wong Jun 20 '21 at 12:55
16
  1. gitk --all
  2. Select the first commit
  3. Right click on the other, then diff selected → this
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
geekbytes0xff
  • 213
  • 2
  • 7
12

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AldaronLau
  • 1,044
  • 11
  • 15
11

I use gitk to see the difference:

gitk k73ud..dj374

It has a GUI mode so that reviewing is easier.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2647616
  • 111
  • 1
  • 2
11

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

git diff HEAD~2 
Flowkap
  • 729
  • 6
  • 16
8
 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.)
Ritu Gupta
  • 2,249
  • 1
  • 18
  • 11
4

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)
Jacob Abraham
  • 915
  • 9
  • 8
4

I always love using the command line and have user friendly tools (with GUI) at my hand. Best of both worlds. Here is how I do it to compare two commits in Git.

You can show the diff between two commits like the following.

Edit your git config file in a TEXT EDITOR:

git config --global -e 

Set up a proper diff tool (user friendly) like Meld like this in Windows in the Git config file:

[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 can be installed using Chocolatey like this from the COMMAND LINE:

choco install meld

Let's define a shell function to help us compare two sha-s (commits) under [alias] in the TEXT EDITOR:

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

To compare the commits with the help of Meld (or your other favorite diff tool, just type at the COMMAND LINE:

git showchangesbetween somesha123 somesha456

The commit sha-s are easily visible typing

 git log 

for example.

Tore Aurstad
  • 3,189
  • 1
  • 27
  • 22
3

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

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

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.

Hope that helps.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
2

Command below perfectly works for me on Ubuntu 20.04 and git v2.25.1:

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

For the last two commits

git diff HEAD~1 HEAD

by extension to compare 2 commits,that can be for example

git diff HEAD~6 HEAD~3
Matoeil
  • 6,851
  • 11
  • 54
  • 77
0

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
bit_cracker007
  • 2,341
  • 1
  • 26
  • 26
-2

Use this command for the difference between commit and unstaged:

git difftool --dir-diff
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Annadurai
  • 31
  • 1
  • 7