Master Git in 30 Days

Day 05: Understanding the Relationship Between Repository, Working Directory, Objects, and Index

In the process of using Git version control, some very basic concepts must be established so that you can learn more efficiently and meaningfully. With clear and correct conceptual support, it will not only help you learn Git command operations, but more importantly, learning Git-related knowledge will become easier.

Understanding Repository

When we use Git for version control, naturally, we need a “repository” to store this version information. The English word Repository means this. I am used to translating this English word as “儲存庫” (storage repository), representing a space or a folder and a bunch of files used to store all versions.

If you have experience using Git, you should know very well that there are many ways to create a repository. If you want to create a Git repository in any folder, just enter the following command to complete the creation:

git init

Through the following diagram of the process of creating a Git repository, we can clearly know that when we execute git init in the G:\git-demo directory, Git will automatically help us create a so-called Git repository in the .git directory of that directory. Don’t doubt it, this .git folder is a complete Git repository. In the future, all version changes will be automatically stored in this folder.

image

Understanding Working Directory

In the above example, the directory G:\git-demo will automatically become our “working directory”. The meaning of “working directory” is that we are preparing to develop project files, and all future work will be edited in this directory, whether it is adding files, modifying files, deleting files, renaming files, … and all other Git-related operations will be completed in this directory, so it is called “working directory”.

When we operate Git-related command parameters, we usually execute them in the “working directory”.

Since there will be many branch situations when using Git version control, the working directory may switch between different branches. Some git commands will update the files in the working directory when they are executed. For example, when you use git checkout to switch to a different branch, because the directory structure of the current branch is not quite the same as the branch you want to switch to, it is very likely to update the files in your current working directory so that these directories and files under the current working directory are the same as the directories and files under another branch to switch to.

Therefore, keeping the working directory clean at the right time is a basic principle in the version control process, especially when merging in the future. This is especially important. I will further explain the related knowledge in future articles.

Understanding Git’s Data Structure

There are two important data structures in Git, namely “objects” and “index”.

“Objects” are used to save all files and version records in the repository, while “index” is used to save the state of the directory before entering the repository.

About Objects

The so-called “object” is a “special file”. The generation process of this file is very interesting. It takes the content of a file, generates a SHA1 hash value through the content, and then names a file according to this SHA1 hash value.

In the process of using Git for version control, all directories and files to be controlled will first distinguish between “directory information” and “file content”, which we call tree objects and blob objects.

Among them, the blob object uses the original “file content” as the content of the blob file (note: the blob object is actually a physical file), and then performs a SHA1 hash operation on its content to generate a hash id, and then uses this hash id as the file name of the blob file. From this, we can know that a blob object is a file with “only content”. Its file name is generated from the content, so any single blob file usually has no help for version control.

Another tree object is used to store which files are included in a specific folder and what the file name of the blob object corresponding to this file is. In a tree object, in addition to containing the file name and related information of the blob object, it can also contain other tree objects. So tree objects are actually synonymous with “folders”.

Regardless of blob objects and tree objects, these are all objects, and these objects will be stored in a so-called “object storage” (object storage), and this “object storage” is by default in the objects directory of the “repository”, as shown in the following figure:

image

We will talk about the detailed object structure in the upcoming articles.

About Index

The so-called “index” is a frequently changing temporary file, which is usually located in a file named index in the .git directory. Simply put, the purpose of the “index” is mainly to record “which files are about to be submitted to the next commit version”. In other words, if you want to submit a version to the Git repository, then you must first update the index status, and the changes will be submitted.

This index file usually saves the state of a specific version in the Git repository. This state can be represented by any commit object and tree object.

We usually don’t directly edit the .git\index binary file, but operate this index file through standard git commands. The commands for operating the index file are roughly as follows:

  • git add
  • git mv
  • git rm
  • git status
  • git commit
  • git ls-files

Git’s “index” is a medium between “object storage” (object storage) and “working directory” (working directory).

You may have already guessed that the relationship between these concepts that this article wants to elaborate on can be explained by the following 5 steps:

  • To use Git version control, you must first create a “working directory” and “repository”. (mkdir, git init)
  • You must first develop in the “working directory”, you may create directories, create files, modify files, delete files, … and other operations.
  • Then when you want to submit a new version to Git’s “repository”, you must first update the “index” status. (git add, git mv, …)
  • Then Git will decide which files to submit to Git’s “repository” based on the current state of the “index”. (git status)
  • Finally, when submitting changes (git commit), the version information will be written to the “object storage” (at this time, a commit object will be written).

We will talk about the detailed index structure and command operations in the upcoming articles.

Note: Since the concept of tree is very similar to directory, when reading foreign original texts, working directory is often written as working tree!

Today’s Summary

The most important thing about the Git architecture discussed today is the relationship between “objects” and “index”, because without “index” information, Git cannot create versions.

Based on the difference between “objects” and “index”, you should be able to find that “objects” are an “immutable” file type. In principle, any objects written to the “object storage” will not change again, because all objects are generated from the original file content. We can also say this is an “object database” (object database), and this database usually only increases content, and there are fewer cases of “deleting content” or “changing content”. Data is only deleted when executing git gc to clean up garbage data. “Index” is a “mutable” index file used to record the content that the current working directory is ready to commit.

When you step by step approach the core of Git, slowly transforming vague and unclear abstract concepts into concrete conceptual knowledge, you will no longer feel uneasy about Git. Please continue to work hard to learn, success is ahead.