After reading this article, "GIT" has a Taichetaiwu understanding

Source: Internet
Author: User

Git definition: Version management tools

History: Without git, we modified a file to copy a copy of the file to the backup folder, and to create a document to record the current file information and operation Records. When the number of files, manual operation will be a very hard and very large workload.

Now: with git helping us manage these copies, we've saved a lot of human costs and made our job easier. Let's not look at Git as mysterious, the core of Git is to create backup and restore backups, but to derive a bunch of new concepts, a lot of students are trapped in these abstract concepts and can't take advantage of Git, This article is based on the file level to understand how git works and how Git commands are created and restored replicas. , whether it's branch, HEAD, snapshot, warehouse, or workspace, there are files on the hard disk.

Basic knowledge:

1. File system: We can understand the hard disk as a Chinese dictionary, the index in front of the dictionary is the file system, can help us quickly find the exact location of the file content. We know from the operating system to delete files, in fact, just delete the index, the specific contents of the file or on the hard disk.

2.git Snapshot: A snapshot is not a copy of the entire project folder, but a snapshot is simply a document documenting the file structure. We know that Git uses snapshots to manage versions, snapshots are Git's file systems, each snapshot is a file index, each commit creates a snapshot, and a number for the snapshot, which is the head. At work, Git finds a snapshot through the head and a snapshot of the files that are backed up to the repository. The snapshot records the file's profile, including the checksum value (obtained by a series of algorithms for the contents of the file), which can be used to quickly determine whether the contents of two files are the same by comparing the checksum of two files.

3.git Warehouse: The warehouse is the place where the contents of the file are actually stored, and the snapshot/index is associated with the files in the repository. When there is no git, we copy the created copy to a backup folder, which is now replaced by the Git repository, but the Git repository is not simply a copy of the exact file but is compressed, and the compressed files are stored in the. git/objects directory, Direct Open is garbled.

4. Workspaces: Workspaces are other things except the. Git directory. Content that is managed through the operating system's file index. is what we see when we use the computer, what we can edit.

5. Staging Area/buffer: Buffer does not hold file contents, buffer is only a snapshot (index file) in the edit state, this snapshot is not numbered. The commit is to save a copy of the buffer, plus a number (head/version number) to point to the snapshot copy. The snapshot holds all the file information in the project, and the index is associated with the files in the warehouse.

Students can use the following image to understand the above concepts, each of the blocks are stored on the hard disk files, Git is to build such a relational library to manage the version.

Let's not be bothered by the complexity of the lines, you just need to know the head, we move the head pointer to find the snapshot through the head number, and then the snapshot to find all the files of this head.

The Git command understands:

1.Status

1.1, contrast buffer with the work area, the results of the comparison of the main 3 cases:

1.1.1, "delete" buffer record files in the workspace, add will remove the corresponding file index from the buffer, but does not affect the contents of the Git repository.

1.1.2, "Add" workspace existing files in the buffer is not recorded, add when the corresponding file will be copied to the warehouse, and in the buffer to establish an index to the corresponding file in the warehouse.

1.1.3, "Change" to the work area of the file contents of the algorithm to verify that the checksum is different from the buffer record, when add will copy the corresponding file to the warehouse, and update the buffer of the index information.

1.2, the comparison buffer and the current head point to the snapshot, the comparison is also increased, deleted, changed 3 cases:

2.Add

Add performs 2 tasks, the first is to copy the "Increase" and "change" files to the warehouse, the second is to maintain the buffer index, to ensure that the buffer index is consistent with the operating system's file index, the snapshot index points to the files in the warehouse, the operating system index points to the workspace files.

3.Commit

The commit is simple, compare the buffer and the workspace, and when the buffer is the same as the workspace content, save the buffer as a new snapshot, generate 1 numbers for the snapshot, and change the current branch head to this number.

4.Reset

Reset 2 conditions:

4.1. Reset file:reset b86563 b.txt, b86563 the b.txt index in this snapshot is copied to the B.txt index of the buffer. Simply modifying the index of the buffer, without affecting the contents of the file, merely modifies the file's association.

4.2. Reset HEAD:reset b86563

4.2.1, parameter--soft: Just modify the head/version number.

4.2.2, parameter--mixed: Default parameter, modifies the current head/version number, and then overwrites the buffer with the specified snapshot, and the workspace is unchanged.

4.2.3, parameter--hard: Modify the head in the current head parameter, overwrite the buffer with the snapshot associated with the parameter head, and restore the workspace to the workspace state when the snapshot was created, actually comparing the "historical snapshot" with the workspace, the files that are not in the snapshot, deleted from the workspace, The checksum code differs from the file that is not in the workspace, and the associated file (in the warehouse) is located by the snapshot and copied to the workspace.

5,Checkout

Reset 2 conditions:

4.1. Checkout branch:Checkout dev , switch to the Dev branch and modify the current version number to the last version number on Dev. If the dev branch does not exist, create a branch called Dev, and the version number is unchanged.

4.2. Checkout head: Overwrites the buffer with the snapshot associated with the head and restores the workspace to the workspace state at the time the snapshot was created, checkout snapshot and reset--hard do not modify the current HEAD, modify the version number, will "discard" The commit after this version number, can be understood as delete snapshot, for example, we enter the command git log on the 5th version, we will see 1, 2, 3, 4, 5 these head logs, when we modify the current version number 3 and then enter the command git log, we can only see 1, 2, 3 of these head logs.

6,revert

Revert is to copy a snapshot and change head to the number of the newly created snapshot, overwrite the buffer with the snapshot, and restore the workspace to the workspace state at the time the snapshot was created. Equivalent to a checkout +commit

7. Remote Warehouse

Workspace files are editable, git repository files cannot be edited, Git is uploaded to remote repositories or downloaded from remote repositories, not all files are downloaded or uploaded.

7.1. When uploading, the latest snapshot number of the remote warehouse must be included in the local snapshot log, if it does not exist, it proves that the remote repository has been changed since the last download, which requires pull first. Instead, git uploads the locally added files to the remote repository and uploads the new snapshot to the remote snapshot. In Figure 1, we can see how git easily finds new snapshots and files through the head.

7.2, download the same time with the upload ...

If there's anything you don't understand, you can add me qq:158937496.

After reading this article, "GIT" has a Taichetaiwu understanding

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.