Git Step by step– (3) Git object model

Source: Internet
Author: User
Tags sha1 hash using git git commands

The previous article describes a series of operations for the local repository, and we'll look at how git works and introduce the Git object model.

When I first started using git, I didn't understand the Git object model and how it worked, but it was easier to understand it after a while and then get familiar with it.

Here we start with the Git object and then demonstrate the Git object model through an instance.

Git objects

There are four types of objects in the GIT system, and all git operations are based on these four types of objects.

    • "Blob": This object is used to save the contents of a file.
    • "Tree": it can be understood as an object tree, which manages some "tree" and "Blob" objects.
    • Commit: Points to only one tree, which is used to mark the state of a particular point in time for a project. It includes some metadata about the point in time, such as the timestamp, the author of the most recent submission, and the last commit (without this entry for the initial commit).
    • "Tag": Adds a tag to a commit (commit).

SHA1 Hash value

We've introduced Git objects, and in a git system, each Git object has a special ID to represent this object, and this particular ID is what we call the SHA1 hash.

The SHA1 hash is a hash value computed by the SHA1 algorithm (one of the SHA algorithm families), with different SHA1 hashes for objects with different contents. If you've read the previous article, you'll certainly remember how we revoked the update based on the commit ID, where the commit ID is a SHA1 hash value.

Git object Model instances

Let's take a look at the above four objects in an example, and in order to be more clear, this step-by-step shows how the object's relationship changes after a series of operations.

First step: Create a new warehouse and add a "calc.py" file

With "Git log--pretty=raw" You can get the SHA1 hash of each commit, or you can get the hash value of the tree that the commit corresponds to.

Therefore, a commit object typically contains the following information:

    • The hash value representing the commit
    • Hash value pointing to the tree object
    • Author
    • Submitted by
    • Comments

in the research of Git object model, there is a very useful command "Git cat-file", which can query the information of specific object through this command :

    • Git cat-file-t key: This command allows you to see the type of object (blob, tree, commit, or tag) through the hash value of an object
    • Git cat-file-p key: The object's contents can be viewed by its hash value

Let's take a look at the commit, the type and content of the tree object in this commit. As you can see here, in this commit, the contents of the tree object point to a Blob object.

Again, we can look at the type and content of the Blob object, where we can see the contents of the BLOB as the file we are submitting.

So, based on some of the above data, we can see that there are three objects in this commit, and we see the relationship between commit, tree, and blob three objects as follows:

Step two: Update the "calc.py" file and add the Sub function

Continue to use "Git log--pretty=raw" To view the commit log.

Also through "Git cat-file" we can see the type and content of each object, here is a step-by-step, directly give all the object relationship.

One thing to note here is that Perforce, SVN, and CVS belong to the delta file system (Delta Storage Systems), which store only the difference between commits (commit) at a time. For git, it will record the entire contents (snapshot) of each file you submit.

Step three: Add a "app.py"; add "advance" folder, including "__init__.py" and "calc.py"
$ git log--pretty=1419771391 +08001419771391 +0800     Add app.py, __init__.py and calc.py

In the same way, we can get a more complex object diagram:

Summarize

The Git object model is like a git system-specific file system that stores updated content, metadata, and version history information in a specific way.

The Git object model is more familiar with how git works, and with that knowledge, we can analyze what's going on behind Git commands.

Git Step by step– (3) Git object model

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.