When I started using git, I always wanted to compare git with CVS or SVN, but soon found that the idea itself was wrong, and that git was just another species, a species that belonged to the future. Its object storage methods, snapshots, branches, etc., are completely different. Everyone who uses git wants to know about the storage objects, snapshots, and commits of git memory files inside the history and branches. Want to know if it's as powerful as it's legendary?
Git ObjectsStart by creating an empty git repository locally
mkdir in/users/lyc/desktop/test/.git/
You will find that there is only one. git folder in the directory.
. git/|--HEAD|--Config|--Description|--Hooks| |--applypatch-Msg.sample| |--commit-Msg.sample| |--post-Commit.sample| |--post-Receive.sample| |--post-Update.sample| |--pre-Applypatch.sample| |--pre-Commit.sample| |--pre-Rebase.sample| |--prepare-commit-Msg.sample| |--Update.sample|--Info| |--Exclude|--Objects| |--Info| |--Pack|--refs
- The description file is intended for gitweb use only, without concern for it.
- The config file contains configuration options specific to the project, such as the most commonly used user name and mailbox.
- The Info directory holds a global executable file that does not want to be managed in the. gitignore file in the Ignore mode (ignored patterns). The use of this is relatively small, and do not care too much.
- Hooks directory Save the client or server-side hook script, generally we are using the default, rarely change, do not care too much.
Therefore, we need to focus on the other four important files or directories: Head and index files, objects and refs directories, because they are the core of git:
- The Objects directory stores all data content.
- The refs directory stores a pointer to the data (branch) Submission object.
- The HEAD file points to the current branch.
- The index file holds the staging area information.
At the start of a Git repository, the objects directory is empty:
find . git/objects.git/objects.git/objects/info. Git/objects/pack
Git initializes the objects directory while the pack and info subdirectories are created in that directory, but there are no other regular files in the directory. Next, we create a new file Test1.txt, Test1, and add it to the staging area
echo test1 > test1.txt$ git add test1.txt
2 files are found in the. git directory, and the content is byte-coded:
. git/|--Index|--Objects| | --a5| | | --Bce3fd2565d8f458555a0c6f42d0504a848bd5
$ find . git/objects.git/objects.git/objects/a5.git/objects/a5/ Bce3fd2565d8f458555a0c6f42d0504a848bd5.git/objects/info. Git/objects/pack
One more index file under the. git directory under Objects a A5 folder with a file named Bce3fd2565d8f458555a0c6f42d0504a848bd5 under the folder Use the following command to view the hash value of the Test1.txt
$ git hash-object test1.txta5bce3fd2565d8f458555a0c6f42d0504a848bd5
The first 2 bits of the found result are the names of the folders, and the last 38 bits are the names of the files. A5bce3fd2565d8f458555a0c6f42d0504a848bd5 This file is zlib compressed with the git cat-file command to take out the contents of the file storage
Cat-file -p a5bce3fd2565d8f458555a0c6f42d0504a848bd5test1
Content is test1.txt file content look at the index file, just learned that the index file saved the staging area information, with the git ls-files-stage command to view the index content
ls-files--stage1006440 test1.txt
At this point, the index holds the new file snapshot added to Staging area Gitt use ' blob ' + len (content) + ' + ' + content as the file content, the first 2 bits of its SHA1 value as the folder name, and the last 38 bits as the file name
Tree ObjectThen look at the tree object, where the tree object can store the file name and also allow a set of files to be stored. Git stores content in a much simpler way than a UNIX-like file system. All content is stored as a tree or Blob object, where the tree object corresponds to a directory in UNIX, and the Blob object roughly corresponds to the inodes or file content. A single Tree object contains one or more tree records, each containing a SHA-1 pointer to a BLOB or sub-tree object with the permission mode (mode), type, and file name information for that object. Now submit the TEST1.TST as the first time the warehouse is submitted
" First commit " [Master (Root-1file1 insertion (+100644 test1.txt
Found that there are two more folders under the Objects directory, the folder below is the opposite of the SHA1 file
$ find . git/objects.git/objects.git/objects/a3.git/objects/a3/ 951d57b1413275b171d967fa67fd90eecff648.git/objects/a5.git/objects/a5/ Bce3fd2565d8f458555a0c6f42d0504a848bd5.git/objects/c0.git/objects/c0/ Da834e42dcbf7b2b1c4a97925bef105d3863a3.git/objects/info. Git/objects/
Take a look at the content separately
Cat-file -1444534714 +08001444534714 +0800 Cat- file -p c0da834e42dcbf7b2b1c4a97925bef105d3863a3100644 blob A5bce3fd2565d8f458555a0c6f42d0504a848bd5 test1.txt
The first one is the commit object, and the second is the tree object, which can be thought of as the root directory of the Git repository, similar to the "/" in UNIX file systems.
commit (Commit) objectWe create a new folder in the root directory temp, add a file test2.txt below temp, the file content is test2, and submit
mkdir temp$ CD Temp/echo"test2" >-am "Second commit "
Discover objects 4 more folders below, and the Sha-1 file opposite the folder
$Find. git/Objects.git/Objects.git/objects/ -. Git/objects/ -/0cf8328022becee9aaa2577a8f84ea2b9f3827.git/objects/ *. Git/objects/ */592c587f70cf6ec1b99bb382bec2ef92f83396.git/objects/9e.git/objects/9e/7b8054ac3ca530d8e69556dff5903cdcbdc4d3.git/objects/A3.git/objects/a3/951d57b1413275b171d967fa67fd90eecff648.git/objects/A5.git/objects/a5/Bce3fd2565d8f458555a0c6f42d0504a848bd5.git/objects/C0.git/objects/c0/Da834e42dcbf7b2b1c4a97925bef105d3863a3.git/objects/D2.git/objects/d2/5d2289339c751ff3f7e1ef1865a58c71d0f51c.git/objects/Info. Git/objects/pack
View commit history with the git log command
<[email protected]>Date: £ º + 0800 <[email protected]> Date: One by one: : + 0800 first commit
Found the last submitted Sha-1 file is d25d2289339c751ff3f7e1ef1865a58c71d0f51c, and the corresponding file is
. git/objects/d2/5d2289339c751ff3f7e1ef1865a58c71d0f51c
We'll see what's in d25d2289339c751ff3f7e1ef1865a58c71d0f51c.
Cat-file -1444542840 +08001444542840 +0800 Second Commit
The tree 35592c587f70cf6ec1b99bb382bec2ef92f83396 is the tree object (that is, the root directory) to which this commit refers to the file is
. git/objects//592c587f70cf6ec1b99bb382bec2ef92f83396
The parent a3951d57b1413275b171d967fa67fd90eecff648 is the second commit pointer to the first commit object author and committer are the authors who are the submitter again Contents of the 35592c587f70cf6ec1b99bb382bec2ef92f83396 file
Cat-file -p 35592c587f70cf6ec1b99bb382bec2ef92f83396040000 Tree 9e7b8054ac3ca530d8e69556dff5903cdcbdc4d3 temp100644 blob A5bce3fd2565d8f458555a0c6f42d0504a848bd5 Test1.txt
The tree object also points to a tree object and a Blod object that represents the SHA1 file for the temp directory and the Test1.txt file. 040000 Tree The corresponding file for 9e7b8054ac3ca530d8e69556dff5903cdcbdc4d3 temp is
. git/objects/9e/7b8054ac3ca530d8e69556dff5903cdcbdc4d3
and continue looking at the contents of the tree object 9e7b8054ac3ca530d8e69556dff5903cdcbdc4d3.
Cat-file -p 9e7b8054ac3ca530d8e69556dff5903cdcbdc4d3100644 blob 180cf8328022becee9aaa2577a8f84ea2b9f3827 Test2.txt
There is only one pointer to the Blob object in the file content (that is, the SHA1 file for the Test1.txt object), and the corresponding file is
. git/objects//0cf8328022becee9aaa2577a8f84ea2b9f3827
Thus, the relationship between the newly added 4 files has been found. Two commit relationships This is the Git object system, which is essentially a Key-value content addressing file system. When a file or directory is added to a git trace using the git add command, a Blob object or tree object is created when a commit is committed with a git commit, and the commit contains the submitter information. Also contains a tree object that points to the repository root directory. The tree then contains a BLOB object that points to the file, or a subdirectory tree object reference article
git design philosophy