Finding diff between current and last version

Git

Git Problem Overview


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

git diff last version:HEAD

Git Solutions


Solution 1 - Git

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 ^.

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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".

Solution 5 - Git

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

Solution 6 - Git

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

git diff --cached --color

Solution 7 - Git

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

Solution 8 - Git

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>

Solution 9 - Git

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

Solution 10 - Git

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

Solution 11 - Git

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".

Solution 12 - Git

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)

Solution 13 - Git

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.

Solution 14 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajeevView Question on Stackoverflow
Solution 1 - GitFrancisco PugaView Answer on Stackoverflow
Solution 2 - GitCharlesBView Answer on Stackoverflow
Solution 3 - GitNightoView Answer on Stackoverflow
Solution 4 - GitTomilov AnatoliyView Answer on Stackoverflow
Solution 5 - GitnaokoView Answer on Stackoverflow
Solution 6 - GitAndrewView Answer on Stackoverflow
Solution 7 - GitcaptinboView Answer on Stackoverflow
Solution 8 - GitparasrishView Answer on Stackoverflow
Solution 9 - Gitbit_cracker007View Answer on Stackoverflow
Solution 10 - GitVasantha GaneshView Answer on Stackoverflow
Solution 11 - GitMandrakeView Answer on Stackoverflow
Solution 12 - GitAlex PunnenView Answer on Stackoverflow
Solution 13 - GitDeyaEldeenView Answer on Stackoverflow
Solution 14 - GitEduardo SantanaView Answer on Stackoverflow