Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare the working tree with a commit?

Tags:

git

diff

I'm using

git diff mycommit 

for comparing my working tree with mycommit, but it seems to ignore files not present in the current index. You can reproduce it as follows:

git init echo A > A.txt; git add .; git commit -m A; git branch A echo B > B.txt; git add .; git commit -m B; git branch B git reset --hard A echo BB > B.txt git diff B 

The output (as of git version 1.7.3.3) is empty. Using --diff-filter=ACDMRTUXB shows "deleted file" which is wrong as well, since the file B.txt exists both in the working tree and in commit B. IMHO, the file should be shown as modified. Surprisingly, it works after adding the file to the index, although it's not the index what gets compared. How can I get the right diff without it?

like image 249
maaartinus Avatar asked Dec 09 '11 23:12

maaartinus


People also ask

How do you compare commits?

You can compare files between two Git commits by specifying the name of the ref that refers to the commits you want to compare. A ref may be a commit ID or HEAD, which refers to the current branch. Let's compare two commits in our Git repository. The above command will perform a diff operation across our two commits.

Which command is used to show changes between commits commit and working tree?

compares the index and the files on the filesystem. The "git-diff-tree" command begins its output by printing the hash of what is being compared. After that, all the commands print one output line per changed file.

How check diff after commit?

To see the diff for a particular COMMIT hash, where COMMIT is the hash of the commit: git diff COMMIT~ COMMIT will show you the difference between that COMMIT 's ancestor and the COMMIT . See the man pages for git diff for details about the command and gitrevisions about the ~ notation and its friends.


2 Answers

A simple graphic might be of help:

enter image description here

So there are three types of diff you can ask for:

  1. git diff --cached This is the difference between what is in the index and the last commit. It shows you that changes that will be in the next commit.

  2. git diff This shows the difference in between the index and the working tree. These are the changes that you have made to files since you last added them to the index. This is why I wasn’t getting any results. I had no changes between the index and the working tree.

  3. git diff HEAD This shows the difference between the files in the working tree and the last commit. There is a gotcha here: if you've made changes, added them to the index, and then backed out these changes in the working tree, you’ll get no results for git diff HEAD (because there is no difference) but you will get output for git diff --cached because there are still changes in the index.

And if you want to compare it against previous commits:

enter image description here

This just compares against the previous commits, but you can replace HEAD~ with any other reference to a commit.

like image 176
Abizern Avatar answered Oct 03 '22 20:10

Abizern


The question is: I have added some file in my working tree, that exists in the other commit. Why is git diff <commit> not showing me the difference between the file content that exists in my working tree and the content of the file that is in the other commit.

This is because the new file that you added is untracked and hence, well, git doesn't track it.

Try git diff HEAD you won't see that you have added B.txt in your working tree.

Basically, diff does not take into account untracked files. That is why you see that the file is deleted, because the diff is same as git diff B A

Now, if you were to add the file, you will see the expected result, because git is tracking it now.

Say, you committed the file and then modify the content in the working tree:

Now, git diff HEAD will show the difference between working tree and HEAD, showing the change you have done in your working tree on tracked files. Same with git diff B

like image 34
manojlds Avatar answered Oct 03 '22 21:10

manojlds