These are the helpful commands for finding out the state of your working directory and index:
- git status: Show differences between the working directory, index and the most recent commit.
- git ls-files: Show what files are in the git index
Let’s start with a new git repository:
$ git init $ ls
git ls-files shows no files are in the git index:
$ git ls-files
git status agrees nothing is going on here:
$ git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)
Let’s run the first command shown in What’s The Deal With The Git Index? (Diagram 1) to create the README file and see what git ls-files and git status have to say.
git status noticed the new README file is in the working directory, but is not in the index:
$ echo This is the README file. > README $ git ls-files $ git status # On branch master # # Initial commit # # Untracked files: # (use "git add ..." to include in what will be committed) # # README nothing added to commit but untracked files present (use "git add" to track)
You might want to re-create the session, but also see the output of the git ls-files and git status commands.
Next, let’s add the file README to the index and see the results:
$ git add README $ git ls-files README $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached ..." to unstage) # # new file: README #
git status now has the README file set to be committed to the repository the next time git commit is used.
Continuing below, after the README file is committed to the repository:
- The README file remains in the index (as shown by the git ls-files command)
- git status now shows there are no modified or new files in the working directory:
$ git commit -m'Initial commit' [master (root-commit) 7ca249a] Initial commit Committer: Tim Flagg <firstname.lastname@example.org> 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 README $ git ls-files README $ git status # On branch master nothing to commit (working directory clean)
Note that the Changes to be committed section at the bottom of the git status output does not list all files that are in the index. Rather, the changes to be committed section of git status reports what is different in the index in comparison to the repository. To see a list of all files in the index, the git ls-files command is used.
Next, adding another line to modify the README file results in:
$ echo One more line. >> README $ git ls-files README $ git status # On branch master # Changed but not updated: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: README # no changes added to commit (use "git add" and/or "git commit -a")
- The README file was changed in only the working directory The README file was not changed in the index.
- The version of README in the index is the older version of README without the extra line since a git add has not been done.
- The README file that is listed as being in the index from git ls-files is the older version of the README file with just one line.
- git status reported that it noticed you changed README in the working directory, but the working directory version of README is not in the index to be committed: “Changed but not updated: … modified: README“.
The next steps would probably be to do a git add README and then git commit.