16

I found the following command in a shell script:

 git diff-index --quiet HEAD --; 

What does this command mean/do?

1
  • Try running git help diff-index. Commented Feb 3, 2015 at 10:11

1 Answer 1

22

It can be used to help determine whether there are any changes in a working tree by comparing your working tree to the HEAD revision. The --quiet means to silence the output and alter the exit code based on whether the tree is modified (exits with 1) or is not modified (exits with 0). The -- is used to separate paths from the rest of the argument. This helps Git know that HEAD is actually the name of a treeish, rather than confuse it with the name of a file should a file called HEAD exist in your tree.

The short form: if the script is using set -e, then the script will exit with an error if your working tree has changes in it.

Sign up to request clarification or add additional context in comments.

5 Comments

Why not just use git diff --quiet, though?
In Git, there's a difference between commands meant for the end user (porcelain) and for scripts (plumbing). git diff is a porcelain command whereas git diff-index is plumbing. You could probably get by with using git diff here if you really wanted to though.
@StuartP.Bentley Here's a list of plumbing commands, just for reference.
Believe me, I know about Git's plumbing commands. What I'm trying to ascertain is, is there a reason that a script would use git diff-index --quiet HEAD -- instead of git diff --quiet, other than a predilection for plumbing over porcelain? (The latter was added in Git 1.5.1, so they're both nearly as old as Git itself.)
@StuartP.Bentley Outside of git diff-index being a plumbing command, no I don't know of any other reason to chose it over git diff in this case. There's no performance benefit or the like since git diff essentially calls git diff-index under the hood in this case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.