What is the difference between the working directory and the git Index? What is the difference between the working directory and the git Index? git git

What is the difference between the working directory and the git Index?


The trick is:

when you add (git add) to the index, you don't have to commit right away

So if you add some super complex function, and then proceed to change and... finally break it completely, you still can commit, because what is in your index (what you have added 10 minutes ago before break it with further failed modifications) is not what is currently in your working tree (which is hopelessly broken right now).

So it can help adding from time to time to the index a current development effort, knowing that you can commit at any time the last "stable" state you have indexed.


The other way what is committed is not what is in your working tree is when you git add --patch:

Interactively choose hunks of patch between the index and the work tree and add them to the index.
This gives the user a chance to review the difference before adding modified contents to the index.

You can add portion of your current file to the index (like one of the three functions you are writing), and then commit only that.


The index is a copy of the directory tree managed by git. Initially, it is a copy of what is in the HEAD commit. git add copies files from the working directory to the index. git commit creates a new commit from what is in the index.

The index is like a buffer-- it is not stored in the git history but access to it is controlled by git (unlike your working directory, which can be accessed in any number of ways). git commits from the index so what is committed is something that git controls.


The answer in your particular case is that you are understanding the documentation correctly, but using the "shortcut" command to commit your entire working directory.

If you run git commit -a -m "Message" then your working directory is treated like it is the staging area. This is convenient sometimes, but you lose the ability to use the index as designed. Try the following command:

git commit -m "Message"

If you do this instead, you can use the staging area to commit only part of the changes you have made to your working directory.