873

Using Git, how can you find the difference between the current and the last version?

git diff last version:HEAD
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajeev
  • 44,985
  • 76
  • 186
  • 285
  • 1
    If you use GitHub for the cloud location of your repo it is very simple: navigate to your project and click on the heading "commits" in the table that displays your project – David Lundquist May 29 '17 at 19:20
  • 3
    The meaning of "current and the last version" really should be clarified in the question. – faintsignal Feb 05 '19 at 16:36
  • You may want to look at this discussion too: https://stackoverflow.com/questions/46446901/how-can-i-see-local-history-changes-in-visual-studio-code – Alma Rahat Oct 09 '22 at 13:13

14 Answers14

1531

I don't really understand the meaning of "last version".

As the previous commit can be accessed with HEAD^, I think that you are looking for something like:

git diff HEAD^ HEAD

That also can be applied for a :commithash

git diff $commithash^ $commithash

As of Git 1.8.5, @ is an alias for HEAD, so you can use:

git diff @~..@

The following will also work:

git show

If you want to know the diff between head and any commit you can use:

git diff commit_id HEAD

And this will launch your visual diff tool (if configured):

git difftool HEAD^ HEAD

Since comparison to HEAD is default you can omit it (as pointed out by Orient):

git diff @^
git diff HEAD^
git diff commit_id

Warnings

  • @ScottF and @Panzercrisis explain in the comments that on Windows the ~ character must be used instead of ^.
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
  • I wanted something like committed version and version before it...git diff head head-1 – Rajeev Mar 28 '12 at 09:13
  • 7
    As of Git 1.8.5, `@` is an alias for `HEAD`. And since `~` and `^` are the same when only going one commit back, I find `git diff @~..@` much easier to type. – Andrew Sep 22 '14 at 01:47
  • 99
    @Andrew `git show` is easier still, since `@~..@` is the default thing to show. – amalloy Nov 06 '14 at 04:28
  • @amalloy Ah! That's right, `HEAD` is implicit for a lot of subcommands. – Andrew Nov 06 '14 at 06:05
  • git difftool HEAD^ HEAD does nothing at all for me. Am I missing something? – Scorb Sep 08 '16 at 02:44
  • @ScottF Do you have your difftool configured? http://stackoverflow.com/questions/6412516/configuring-diff-tool-with-gitconfig – Francisco Puga Sep 08 '16 at 06:28
  • `HEAD^` does not work in my case, but `HEAD~1` does, and so does `git show`. I was thinking this may be a difference between Git 1.x and Git 2.x, but @ScottF may have found the real reason. Maybe the answer could be edited to contain some information about this as well? – Panzercrisis Sep 22 '16 at 14:01
  • git show is absolutely useful if you just wanna get an idea of whatz goin on! – Ceylan Mumun Kocabaş May 19 '17 at 14:35
  • 6
    The problem with `git show` is that if `HEAD` is a merge commit you won't get what you expect since the merge commit itself may not have any changes itself. `git diff HEAD^ HEAD` will show the actual changes between the versions – RubenLaguna Apr 24 '18 at 07:17
  • 3
    Remark: For Windows Command Prompt, `^` is an escape character. Could type `^^` to represent a `^` – Johnny Wong Feb 27 '20 at 03:34
  • git diff HEAD~1 HEAD (MacOS work as expected). git diff HEAD^ HEAD (no matches found: HEAD^) – Jorge Tovar Dec 27 '21 at 14:26
196

Assuming "current version" is the working directory (uncommitted modifications) and "last version" is HEAD (last committed modifications for the current branch), simply do

git diff HEAD

Credit for the following goes to user Cerran.

And if you always skip the staging area with -a when you commit, then you can simply use git diff.

Summary

  1. git diff shows unstaged changes.
  2. git diff --cached shows staged changes.
  3. git diff HEAD shows all changes (both staged and unstaged).

Source: git-diff(1) Manual Page – Cerran

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 18
    And if you always skip the staging area with `-a` when you commit, then you can simply use `git diff`. <1> **`git diff`** shows **unstaged** changes. <2> **`git diff --cached`** shows **staged** changes. <3> **`git diff HEAD`** shows **all** changes (both staged and unstaged). Source: [git-diff(1) Manual Page](http://git-scm.com/docs/git-diff) – Cerran Feb 20 '14 at 13:16
  • What is the name of "the current unstaged version" in git? Is there actually a name? – Mathieu CAROFF Jan 17 '19 at 08:48
  • `git diff HEAD --stat` if you're looking for the number of lines changed. – Brunno Vodola Martins Jul 14 '22 at 01:34
142

As pointed out on a comment by amalloy, if by "current and last versions" you mean the last commit and the commit before that, you could simply use

git show
Community
  • 1
  • 1
Nighto
  • 3,994
  • 3
  • 23
  • 28
  • 16
    Use `git show HEAD~1` to show the last-but-one commit, and `git show HEAD~2`, etc. for older commits. Show just a single file via `git show HEAD~2 my_file`. – Florian Brucker Mar 03 '16 at 10:43
  • Perhaps address *[RubenLaguna's note](https://stackoverflow.com/questions/9903541/finding-diff-between-current-and-last-version#comment87007426_9903611)* in the answer? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Mar 30 '22 at 16:50
66

Difference between last but one commit and last commit (plus current state, if any):

git diff HEAD~

or even (easier to type)

git diff @~

where @ is the synonim for HEAD of current branch and ~ means "give me the parent of mentioned revision".

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
63

You can do it this way too:

Compare with the previous commit

git diff --name-status HEAD~1..HEAD

Compare with the current and previous two commits

git diff --name-status HEAD~2..HEAD
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
naoko
  • 5,064
  • 4
  • 35
  • 28
18

Just use the cached flag if you added, but haven't committed yet:

git diff --cached --color
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrew
  • 3,733
  • 1
  • 35
  • 36
14

Quick and simple, assuming you're in the master:

    git diff (checkout_id):file.txt file.txt

Example:

    git diff asdfioei91819280din198:file.txt file.txt
RAFisherman
  • 889
  • 1
  • 8
  • 9
  • 2
    Note that after the `(checkout_id):` you need a relative path to the filename from the root of the repo. For me I tried the above from the directory the file was in, and it failed, until I changed it to `git diff 3d44feb544150cf35b2a99d5917e294e10596f8e:./file.txt file.txt` Also, OP's original intent isn't clear, but this answer is the only one that addresses if you want the "difference between the current and last version" OF A FILE. I originally tried `git diff HEAD~1 -- file.txt` but it didn't work because last change to that file was 10 commits ago. – Starman Oct 07 '20 at 15:57
  • 1
    This was exactly what I was looking for when I found this question. Thanks! – Sony Santos Mar 19 '22 at 14:45
9

If you want the changes for the last n commits, you can use the following:

git diff HEAD~n

So for the last 5 commits (count including your current commit) from the current commit, it would be:

git diff HEAD~5

Vasantha Ganesh
  • 4,570
  • 3
  • 25
  • 33
8

Firstly, use "git log" to list the logs for the repository.

Now, select the two commit IDs, pertaining to the two commits. You want to see the differences (example - Top most commit and some older commit (as per your expectation of current-version and some old version)).

Next, use:

git diff <commit_id1> <commit_id2>

or

git difftool <commit_id1> <commit_id2>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
parasrish
  • 3,864
  • 26
  • 32
7

If the top commit is pointed to by HEAD then you can do something like this:

commit1 -> HEAD
commit2 -> HEAD~1
commit3 -> HEAD~2

Diff between the first and second commit:

git diff HEAD~1 HEAD

Diff between first and third commit:

git diff HEAD~2 HEAD

Diff between second and third commit:

git diff HEAD~2 HEAD~1

And so on...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bit_cracker007
  • 2,341
  • 1
  • 26
  • 26
2

I use Bitbucket with the Eclipse IDE with the Eclipse EGit plugin installed.

I compare a file from any version of its history (like SVN).

Menu Project Explorer → File → right click → TeamShow in history.

This will bring the history of all changes on that file. Now Ctrl click and select any two versions→ "Compare with each other".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mandrake
  • 411
  • 4
  • 2
2

This will also work for tags (remove the 'uniq' below and other parts if you need to see all changes):

 git diff v1.58 HEAD 

The below is the same, and that could be useful for continuous integration (CI) for microservices in a monolithic repository:

git diff v1.58 HEAD  --name-only | sort -u | awk 'BEGIN {FS="/"} {print $1}' | uniq
<Folder Name> 

(Credit - https://dzone.com/articles/build-test-and-deploy-apps-independently-from-a-mo)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Punnen
  • 5,287
  • 3
  • 59
  • 71
1

to show individual changes in a commit, to head.

git show Head~0

to show accumulated changes in a commit, to head.

git diff Head~0

where 0 is the desired number of commits.

DeyaEldeen
  • 10,847
  • 10
  • 42
  • 75
0

If last versions means last tag, and current versions means HEAD (current state), it's just a diff with the last tag:

Looking for tags:

$ git tag --list
...
v20.11.23.4
v20.11.25.1
v20.11.25.2
v20.11.25.351

The last tag would be:

$ git tag --list | tail -n 1
v20.11.25.351

Putting it together:

tag=$(git tag --list | tail -n 1)
git diff $tag
Eduardo Santana
  • 5,780
  • 3
  • 19
  • 21