What's the difference between the index, cached, and staged in git?

Viewed 9173

Are these the same thing? If so, why are there so many terms?!

Also, I know there is this thing called git stash, which is a place where you can temporarily store changes to your working copy without committing them to the repo. I find this tool really useful, but again, the name is very similar to a bunch of other concepts in git -> this is very confusing!!

3 Answers

add/index/stage/cache

The index file:

  • is an index of all tracked files
  • used as a staging area for commits
  • contains cached SHA1 hashes for the files (speeds up performance)

From man git:
$GIT_INDEX_FILE sets the index file. If not specified, $GIT_DIR/index is used.

The index file is a binary file.
When opened by nvim, here is part of the screenshot:
enter image description here

When opened by nvim with a plugin named fugitive:
enter image description here
The content is very similar to
the output of git status, instead of strings .git/index

related command

  1. git add ("a file is added/staged to the index")
  2. git restore --staged ( --staged | restore the index )
  3. git rm --cached (--cached | only remove files from the index )

stage vs track

  • Untracked changes are not in Git.
  • Unstaged changes are in Git, but not marked for commit.
  • Staged changes are in Git and marked for commit.

git ls-files

            1.
                -c, --cached  ( cache:  obsolete for index) 
                    Show cached files in the output
                    (default)

                -s, --stage
                    Show staged (git add) files'  
                                            mode bits,
                                            object name
                                            stage number  
                -u, --unmerged
                    (forces --stage)
                    Show unmerged files in the output

                -d, --deleted 

                -m, --modified   

                -k, --killed
                    Show files on the filesystem
                    that need to be removed due to
                    file/directory conflicts
                    for checkout-index to succeed. 
            2.
            -o, --others
                other (i.e. untracked) files

                --directory
                    If a whole directory is classified as "other",
                    show just its name  (with a trailing slash)
                    and not its whole contents.

                    --no-empty-directory
                        Do not list empty directories.
                        Has no effect without --directory.

Further Reading

https://github.blog/2021-11-10-make-your-monorepo-feel-small-with-gits-sparse-index/

overview

enter image description here

Related