Search code examples
gitgithubfilesystemsrecovery

How does Git retrieve deleted files which are deleted from both local and remote repositories?


It astonished me of how files which are deleted once and overwritten with new files can be retrieved anytime we want. Does Git store every instance of files on the server permanently?


Solution

  • This is my understanding. Git stores blobs as as hash names. A file is stored as a list of hashes and is created on your worktree by concatinating the blobs that it is made up of (for small files there is usually just one blob) . Different version of the same file can be made by concatinating different blobs. Blobs are stored in the .git directory and kept as long as there is a reference to them. ie if any tag, commit, reflog, stash etc. contains a file (or anything else) that references the blob. 'git gc' will delete unreferenced blobs that are more than two weeks old.