‹ All Git Interview Questions questions

Git Interview Question 1: Diff Working Tree Index

Git Interview Questions · Test 1 · question 1 of 10

The Three Treeseasy

You add a line X to app.js and run git add app.js. Before committing, you add a second line Y to the same file and save it. You have not staged anything else.

An interviewer asks: if you now run plain git diff with no arguments, what will it show for app.js?

Select one answer
Show answer and explanation

Answer:

  • A. Only line Y, because it compares the working tree against the index

Git tracks three "trees": HEAD (the last commit), the index (the staging area for the next commit), and the working tree (the files on disk). After git add, the index holds the version with X. The working tree holds the version with X and Y. Plain git diff compares the working tree to the index. It shows the changes you could still stage but haven't yet, which here is only Y.

Option B describes git diff HEAD, which compares the working tree against the last commit and would show both lines.

Option C describes git diff --staged (a synonym of --cached), which compares the index against HEAD and shows what the next commit would contain. Here that is only X.

Option D is wrong. Staging a file does not hide it from git diff. The file has changes beyond the staged version, and git diff reports those.

Reference: git-diff

Practice Test 1 ›

Study guides for Git Interview Questions