Git object type and Storage Structure

Source: Internet
Author: User
Tags sha1

Git version 1.9.6


1. Basic Object Types of Git

2. View git Object Information

3. Understand the GIT Storage Structure


1. There are four basic object types in git, forming a more advanced git data structure:

  • Blobs
    Each blob represents a (Version) file. Blob only contains the data of the file and ignores other metadata of the file, such as the name, path, and format.

  • Trees
    Each tree represents the information of a directory, including the blobs, subdirectories (corresponding to the Child trees), file names, paths, and other metadata under the directory. Therefore, for directories with subdirectories, git stores nested trees.

  • Commits
    Each commit records all the metadata for submitting an update, such as the directed tree, parent commit, author, Submitter, submission date, and commit log. Each commit is directed to a tree object, which records the directory information for the next commit. A commit can have multiple (at least one) parent commits.

  • Tags
    A tag is used to assign a name that is easy for developers to remember to an object of the above type. It is usually used for a certain commit.

Terms 1,

Corresponds to the last submission in the current status. Head corresponds to an index, not a branch. Similarly, fetch_head.

Illustration:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/A2/wKiom1RV01mhfTD2AAGqihwodD4244.jpg "Title =" comm.jpg "alt =" wkiom1rv01mhftd2aagqihwodd00004.jpg "/>

All objects are stored in. Git/objects/, such as using commands.

$ find .git/objects -type f.git/objects/03/3faad6dd01bd59594f447f159b307553d87db9.git/objects/0f/e9ab15a2da28c2cdef6c4f92a1ff0067424f8e.git/objects/13/be0240b1698df5f5d53123b3ac59cceb4b3ee0.git/objects/17/533d40936b9abc18577b2c940c083031d4a27c.git/objects/17/eb2d95ca405ce1005ebb7274e995cfbad1e43b.git/objects/1a/fd63e52c13dac81819b55d3e56272e0e930552.git/objects/1c/59540ce36561075575652b8642eb40406f9e6a.git/objects/25/01ab8ba1f7c3dbf91023cb8c39592b9808ab5c

Git uses the first two digits of the sha1 value to store objects in different directories.

033faad6dd01bd59594f447f159b307553d87db90fe9ab15a2da28c2cdef6c4f92a1ff0067424f8e。。。。。。

In the corresponding directory of objects ::

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/A2/wKioL1RV1O_SUVHyAAFDCgMV3R4700.jpg "Title =" c1.jpg "alt =" wkiol1rv1o_suvhyaafdcgmv3r4700.jpg "/>

Run git cat-file to view the object values and content. Generally, different objects can be distinguished using the first few digits (not all required) of the sha1 value. Therefore, you can use git cat-file-T 033faa in the preceding command.

Git cat-file-T <sha1> # view the object Value git cat-file-P <sha1> # view the object content

2. view object information

2.1 view object values

git hash-object filename

2.2 view object content

git show <SHA1>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/A2/wKioL1RV19HjRoTmAAHHBwQB65U227.jpg "Title =" c1.jpg "alt =" wkiol1rv19hjrotmaahhbwqb65u227.jpg "/>

2.3 view the commit history

$ git log --pretty=oneline

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/A2/wKioL1RV2TCQuXH4AAJWb-09qrg733.jpg "Title =" c1.jpg "alt =" wKioL1RV2TCQuXH4AAJWb-09qrg733.jpg "/>

View commit content

git show <SHA1>

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/A2/wKioL1RV5NWDxjD9AAFWPL-y4NY176.jpg "Title =" c1.jpg "alt =" wKioL1RV5NWDxjD9AAFWPL-y4NY176.jpg "/>

2.4 view the tree to which commit points

Git rev-Parse <sha1> # convert various reference notation into hash values.

Or

$ git cat-file -p   25220245febc99c82463951cf7b0011c25e8399e

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/A3/wKioL1RV5ePxuOeDAAJ_5gvv54U413.jpg "Title =" c1.jpg "alt =" wkiol1rv5epxuoedaaj_5gvv54u413.jpg "/>

3. deep understanding of git Storage

The above describes the data type and structure of git. What is its storage logic structure?

3.1. Git stores all git data:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/A3/wKioL1RV66ui1n59AAFGxrMxeSU277.jpg "Title =" c1.jpg "alt =" wkiol1rv66ui1n59aafgxrmxesu277.jpg "/>

Head corresponds to the index, which first leads to the master file, which records the hash value of the latest commit object

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/A4/wKiom1RV7BGSHHMCAAIvcEfiqGs921.jpg "Title =" c1.jpg "alt =" wkiom1rv7bgshhmcaaivcefiqgs921.jpg "/>

3.2. Check whether the object is a commit object? (Yes)

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/A3/wKioL1RV7M_hQzuvAAB8ERAohwk580.jpg "Title =" c1.jpg "alt =" wkiol1rv7m_hqzuvaab8eraohwk580.jpg "/>

View the content of this commit

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/A3/wKioL1RV7P_xwTShAAFfA6VA2NM725.jpg "Title =" c1.jpg "alt =" wkiol1rv7p_xwtshaaffa6va2nm725.jpg "/>

3.3 A tree object and a parent object are the key. Tree indicates all content under the current commit object, and the parent object points to the previous commit object;

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/A3/wKioL1RV7beziLqAAAEYqc7rdqI283.jpg "Title =" c1.jpg "alt =" wkiol1rv7bezilqaaaeyqc7rdqi283.jpg "/>

The comparison shows that the hash of the current parent object is consistent with that of the previous commit object;

What if I want to see the value of the previous three commit statements?

$ git rev-parse HEAD~3445996a2fdbfbda904b030d7ab12ba19579669b8

View the tree content under the current commit, that is, the Blob content.

Method 1. First-level search for git hash values

$ Git cat-file-P 252202 # view the content of the current commit object $ git cat-file-P b1ca8027d72b7 # view the tree content of the current commit object $ git show 5e3c993fc7 # View the content of a specific blob under the tree of the current commit object

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/A3/wKioL1RV736A2ZIXAAO0PM8y4SI383.jpg "Title =" c1.jpg "alt =" wkiol1rv736a2zixaao0pm8y4si383.jpg "/>

Method 2: Path:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/A4/wKiom1RV74mxUkDtAAGBXMWUHXo774.jpg "Title =" c1.jpg "alt =" wkiom1rv74mxukdtaagbxmwuhxo774.jpg "/>

3.4 what if I want to view the data under the previous commit object?

Method 1. Obtain the value of the previous commit object (head)

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/A3/wKioL1RV8pjSTWcKAAMR_facrSw857.jpg "Title =" c1.jpg "alt =" wkiol1rv8pjstwckaamr_facrsw857.jpg "/>

The test directory is missing from the current commit, but the file remains unchanged;

Method 2: view files in the same path:

[email protected] /D/User/leo/Desktop/git/.git (GIT_DIR!)$ git show HEAD~:hello.rbputs "hello world!"


TIPS: now that you know the storage structure, what should I do if I want to roll back the code to the previous status? (Restore the current commit to the previous one)

Method 1: git reset -- hard commit-ID: roll back to commit-ID, and remove all committed commit after commit-id.

Method 2: git reset -- hard head ~ 2: roll back the last three submissions

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/A4/wKiom1RV9RKDBa2oAAKv_soo4vY319.jpg "Title =" c1.jpg "alt =" wkiom1rv9rkdba2oaakv_soo4vy319.jpg "/>

This article is from the "Big Brother" blog, please be sure to keep this source http://lansgg.blog.51cto.com/5675165/1570942

Git object type and Storage Structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.