The Foundation of Git Series--commit

Posted by abc123 on Tue, 02 Jul 2019 21:24:43 +0200

brief introduction

The previous section introduced the add command, which adds the modification of the current working directory to the temporary storage area. Then, when stored in the temporary storage area, it should be submitted to the Git database for version management in time. This is the time for this article to introduce the commit command.

commit submission

Commit, as its name implies, is used to submit the contents of the temporary area to the Git local database. Note that each commit only acts on the current one. branch

1. Grammar

git commit [options]... (for more details: git-commit)

2. Submission

When git commits, it saves a submission object, which contains the following contents:

  • A pointer to a snapshot of temporary content (the temporary operation calculates the checksum for each file using the SHA-1 hash algorithm and generates a blob object)
  • Author's name and mailbox
  • Information entered at submission time and pointers to its parent object (the submission object generated by the first submission has no parent object, the submission object generated by the ordinary submission operation has one parent object, and the submission object generated by the merging of multiple branches has multiple parent objects)

Primary use

With the add command, we have added all the changes to the current working directory to the temporary area (you can confirm with git status whether they have been added to the temporary area), and then run the submit command:

➜  test git:(master) ✗ git commit -m "first commit"
[master (root-commit) 6fffa3a] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
➜  test git:(master) git log
commit 6fffa3a925f1454195a297c7b5373c273eeaa5aa
Author: zouziwen <zouziwen@meituan.com>
Date:   Mon May 8 17:35:28 2017 +0800

    first commit

From the information returned after commit execution, it can be concluded that the commit-id is 6fffa3a, the commit-message is first commit, and a file is inserted; and as the above example, it can be passed through the master branch. git log To query the submission history of the current working directory

Advanced Use

Like the add command, Git also provides a set of high-level commands to complete the commit operation, which is executed downward on the basis of the add high-level command in the previous section.

➜  test git:(master) git write-tree        // Spanning tree in current directory
9d223a574a4b40a55bbf02a047c042c6c0b889a9
➜  test git:(master) echo "first commit" | git commit-tree 9d223a574a4b40a55bbf02a047c042c6c0b889a9   // Submit tree
85f186d00fe88ab272053dfe0d80b1f26dec6464
➜  test git:(master) git update-ref refs/heads/master 85f186d00fe88ab272053dfe0d80b1f26dec6464  //Update the commit of the current branch
➜  test git:(master) git log 
commit 85f186d00fe88ab272053dfe0d80b1f26dec6464
Author: zouziwen <zouziwen@meituan.com>
Date:   Mon May 8 18:33:45 2017 +0800

    first commit

Git object

➜  test git:(master) git cat-file -t cb19826acbc17d65e6b492abf2b5b10930c184f3
blob
➜  test git:(master) git cat-file -t 9d223a574a4b40a55bbf02a047c042c6c0b889a9
tree
➜  test git:(master) git cat-file -t 85f186d00fe88ab272053dfe0d80b1f26dec6464
commit

As shown in the above command, the contents of Git object storage are obtained separately as follows:


Git object

Git generates a blob-object for each file, and a tree-object is provided to solve the problem of file name preservation. Git stores content in a UNIX-like file system, but with some simplification. All contents are stored in the form of tree objects and data objects, where tree objects correspond to directory items in UNIX and data objects roughly correspond to inodes or file contents. A tree object contains one or more tree entry records, each record contains a SHA-1 pointer to a data object or a subtree object, as well as the corresponding pattern, type, file name information.

Git generates a commit object for each commit, and each commit object corresponds to a tree, which corresponds to the current directory root directory.


All objects in the root directory

git log

The git log command outputs the submission history of the current work branch. By default, without any parameters, the GIT log lists all updates by submission time, with the most recent updates at the top. This command lists the SHA-1 checksum for each submission, the author's name and e-mail address, the submission time, and the submission instructions.

As for the use in the official website documents described very clearly, there is no need to mention here. Portal: git-log

Last article: The Foundation of Git Series--add

Next article: The Foundation of Git Series--push, pull, fetch

Topics: git Database Unix snapshot