6

Actually, I read a lot of things about git.

I know I can remove cache by git rm --cached . command but I can not understand the concept of git-cache.

What is cache? And where is the location of caching in GIT? Is this cache stored in RAM or in a file? Is cache used to improve performance in git?

What is stored in the cache?

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Alihossein shahabi
  • 4,034
  • 2
  • 33
  • 53
  • 1
    Consider a [Git tutorial](https://git-scm.com/docs/gittutorial) — as knowing how/what this is, and how it is used, is *very important* to using Git. The “cache”, in this context, contains *staged changes*. The contents are stored *on disk, in repo’s .git folder*. – user2864740 Oct 11 '20 at 19:26
  • 1
    Does this answer your question? [What does 'adding to the index' really mean in Git?](https://stackoverflow.com/questions/25351450/what-does-adding-to-the-index-really-mean-in-git) – mkrieger1 Oct 11 '20 at 19:28
  • 2
    Despite the option of the command named "cached", the thing you are asking about is called "index". – mkrieger1 Oct 11 '20 at 19:29
  • 1
    The documentation for [git-rm](https://git-scm.com/docs/git-rm) says “Use [the —cached] option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.” The —cached option is also available on other commands such as git diff, which is extremely useful. – user2864740 Oct 11 '20 at 19:33

1 Answers1

10

Git is a fairly old (15+ years) program with a lot of parts and some internal terminology, not all of which has aged very well. The expression the cache, and the spelling of the flag as --cached, is one of those. The most modern term for this entity is the staging area, which reflects how you use it, but the most common term in Git for this entity is the index.

So, in general, if someone or something mentions the index, the staging area, or the cache, they probably mean the same thing:

where is the location of cache in git?

Use git rev-parse --git-dir to find the Git directory. In the Git directory, look for a file named index. But before you do that, check the environment variable $GIT_INDEX_FILE: if that's set, try that path name instead.

In the shell,1 then, it's in the file:

ls -l ${GIT_INDEX_FILE-$(git rev-parse --git-dir)/index}

This file may or may not exist; if it does not exist, there is, at the moment, no index and the cache is empty.


1This assumes /bin/sh or compatible syntax; some shells have different constructs.


Is this cache stored in RAM or a file?

As we just saw, it's in a file. However, Git reads this file, after which it is in RAM (in whatever format is most useful in memory, which differs in general from its appearance on-disk). The file itself may contain directives instructing parts of Git to read other files, as well.

Is cache used to improve performance in git?

Yes: in its role as a cache, the intent is to improve performance. However, the index has more roles than just "as performance-improving cache". Its main role is as "staging area", hence the name staging area. It also has a major function in resolving conflicted merges, for which only the generic term index is useful. (Even then the word index doesn't convey any real information. In a sense, that's actually a good thing, because the contents of the file(s) may change in future versions of Git.)

I know I can remove cache by git rm --cached . command

This doesn't remove the cache itself. It removes entries from the cache (and even then they may only be marked "removed", internally, and there are also on-disk extensions to keep some entries around to be resurrected if needed).

what is cache ?

As a generic term in computing, it's, well, very generic.

What is stored in the cache?

The Git source includes a technical document with details. You can see it in action using git ls-files --stage and git ls-files --debug.

torek
  • 448,244
  • 59
  • 642
  • 775