Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between git diff and gif diff-index

Tags:

When I want to get git diff files, I found someone use

git diff-index --cached  --diff-filter=AM --name-only  HEAD

and if I use

git diff --cached --diff-filter=AM --name-only  HEAD 

can get the same result. So what's the difference between git diff and git diff-index? When you must use git diff-index but not git diff?

like image 326
myyuanyuan5200 Avatar asked Jun 13 '14 04:06

myyuanyuan5200


People also ask

What is git diff index?

git diff-index is a diff against the index or working tree: Compares the content and mode of the blobs found in a tree object with the corresponding tracked files in the working tree, or with the corresponding paths in the index.

What is the difference between git diff and git diff -- staged?

git diff --staged will only show changes to files in the "staged" area. git diff HEAD will show all changes to tracked files. If you have all changes staged for commit, then both commands will output the same.

What does git diff show you?

The git diff command helps you see, compare, and understand changes in your project. You can use it in many different situations, e.g. to look at current changes in your working copy, past changes in commits, or even to compare branches.

What does git diff head do?

The git diff HEAD [filename] command allows you to compare the file version in your working directory with the file version last committed in your remote repository. The HEAD in the git command refers to the remote repository.


2 Answers

git diff-index is a diff against the index or working tree:

Compares the content and mode of the blobs found in a tree object with the corresponding tracked files in the working tree, or with the corresponding paths in the index

git diff is more versatile and can compare two files, or two commits, or (like diff-index) a tree and the index.

In your case, a diff HEAD would indeed diff HEAD against the index, which diff-index does too.

like image 74
VonC Avatar answered Sep 20 '22 14:09

VonC


diff-index is a lower level operation that does less but is much faster because it doesn't look at the content, it only checks metadata like timestamps. Only Linux you can verify this with something like:

  strace -f -e file -o diff-index.log git diff-index HEAD
  wc diff-index.log
  less diff-index.log

Forget about diff-index unless you have some git performance problem to solve.

https://git-scm.com/docs/git-update-index#_using_assume_unchanged_bit

https://public-inbox.org/git/[email protected]/

like image 45
MarcH Avatar answered Sep 19 '22 14:09

MarcH