Home  >  Article  >  Development Tools  >  The underlying processing flow of git

The underlying processing flow of git

王林
王林Original
2023-05-20 09:11:07539browse

The popularity of code hosting platforms such as GitHub and GitLab has made Git a much-discussed version control tool, and more and more people have understood how Git works. However, understanding the appearance of Git is only to use Git. To truly understand Git, you also need to understand the underlying processing flow of Git.

Overview of the underlying structure of Git

Git is a distributed version control system, corresponding to a centralized version control system (such as SVN). Because of the distributed nature of Git, each Git repositories are complete repositories.

The working directory of Git contains two parts: Git warehouse object and working tree. The status of Git warehouse object and working tree can be analogized to the relationship between aliases, hard links and soft links respectively.

Git’s underlying file storage method

Git’s underlying file storage technology is mainly divided into two aspects:

  1. Object storage
  2. Compressible files Usage of format

Object storage

Git saves all code changes as individual objects, among which the key objects are blob, tree and commit. Among them, blob is a snapshot of code content, tree is a snapshot of a set of files and directories, and commit is a snapshot of code changes.

Careful readers will find that these objects are somewhat similar to the inode mechanism in Linux systems. An inode file node can represent a file or directory, and an inode file node contains information such as the disk block number. In Git, blob is the snapshot object of the file content in the inode file node, tree is the snapshot object of the inode directory, and commit is the version snapshot composed of multiple inode file nodes.

In Git, objects are usually represented as SHA1 hashes. The SHA1 hash value is a hexadecimal string of 40 characters. Git uses SHA1 hashes to assign a unique identifier to each version, each file and directory, and each commit.

Use of compressible file format

The bottom layer of Git uses a technology that adds a part of metadata to the file to handle code changes. Metadata is often some intermediate state, such as change information between two commits. This information can be compressed into small files and decompressed when needed.

The default file format used by Git is packfile format. Packfile is a highly compressed Git object storage format that can archive multiple objects into a single file for transfer when Git performs cross-network operations.

Git's underlying core processing process

In the previous content, we have a detailed understanding of Git objects and underlying file storage technology. Next, we will enter the underlying core processing process of Git.

Git initialization process

  1. Create directory.git/
  2. Create subdirectory.git/objects/
  3. Create subdirectory.git/ refs/
  4. Create an empty HEAD file
  5. Create an empty index file

Git’s basic file command

Here we first introduce Git A brief introduction to the various basic file commands:

  1. hash-object command: used to convert files into Git objects.
  2. cat-file command: used to display the contents of Git objects.
  3. ls-tree command: used to display the contents of a certain Git tree.
  4. update-index command: used to add files or directories to Git index.
  5. write-tree command: used to convert Git index into a Git tree object.

Git's submission process

Git's submission process still consists of three fields: Blob, Tree, and Commit.

  1. Blob: Used to represent the metadata of each file in the code, including file name, file type, and of course SHA1 hash value, etc.
  2. Tree: Based on the Blob in the previous step, assemble the corresponding files and directories to form a snapshot tree and save it in a Git node.
  3. Commit: Assemble the above two objects plus the submitted user information to form a version snapshot.

In the above steps, there are some things that need to be paid attention to. For example, when performing Blob conversion, you need to add the -g parameter.

Git's branch process

In Git, branches are independent pointers pointing to the last submitted object. There are two types of branches: local branches and remote branches.

After the local branch is created, adding a new submission will automatically move HEAD to point to the latest submission. During this period, the checkout command is used to switch between different branches. Remote branches refer to a way of collaborating code between different local libraries.

Summary

This article elaborates on the underlying processing process of Git from two aspects: Git's underlying file storage method and Git's underlying core processing process. Through the explanation of Git objects and underlying file storage technology, we understand the underlying architecture of Git. This article also introduces the underlying core processing process of Git, including Git's initialization process, Git's basic file commands, Git's submission process, and Git's branch process. Through an in-depth understanding of the underlying processing flow of Git, we can better understand the operating mechanism of Git and use Git for version control more efficiently.

The above is the detailed content of The underlying processing flow of git. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn