What is git? Internal anatomy

Source: Internet
Author: User
Tags version control system

Git: is currently the world's most advanced distributed version control System (no one).

Git features: Version control management

Version: A sample of a file after each save.

    1. If you modify a file, and you are not sure that the original version may be used later, so you will save the original version and the newly generated version, again modify the second version, you will generate a version, so, about a file, you have each version of the backup. In this way, your management must be extremely inconvenient.

    2. Some parts require your financial colleague to help fill it out, so you copy the file to her on the USB stick (or you may send a copy to her via email), and then you continue to modify the Word file. After a day, the colleague will pass the word file to you again, at this time, you must think, after sending her to you to receive her document, you made the change, had to put your change and her part merger, really difficult.

Centralized version-control system:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/82/E8/wKiom1dj3GvS4x15AAA5bKTA5lg990.png "title=" 0.png " alt= "Wkiom1dj3gvs4x15aaa5bkta5lg990.png"/>

The result: Centralized version control must be networked using version control, when the file is large, vulnerable to network status impact download, upload speed.

Distributed version control system:

Distributed version control system does not have a "central server", everyone's computer is a full version of the library, so that when you work, you do not need to network, because the repository is on your own computer. Since everyone has a full repository on their computers, how can multiple people collaborate? For example, you change the file a on your computer, your colleague also changed the file a on his computer, when you two just push their changes to each other, you can see each other's changes.

Compared with centralized version control system, the security of Distributed version control system is much higher, because everyone's computer has a complete version of the library, a person's computer is broken do not matter, casually from other people to copy one. Centralized version control system central server if there is a problem, everyone can not work.

In the actual use of Distributed version control system, in fact, rarely in the two people on the computer to push the revision of the Repository, because maybe you two are not in a local area network, both computers can not access each other, or your colleague is sick today, his computer did not boot. Therefore, the distributed version control system usually also has a "central server" computer, but the role of this server is only to facilitate "exchange" everyone's changes, without it everyone also work, just exchange changes inconvenient.

Linus spent two weeks writing a distributed version control system in C, which is git! Within one months, the Linux system's source code has been managed by Git! In 2008, the GitHub site came online, providing free git storage for open source projects, with countless open source projects migrating to GitHub, including Jquery,php,ruby and more.

User registration of an account can be local git to the remote GitHub repository to save, as long as there is a network, anytime, anywhere can be from the remote side to get or push their own file data, so that the advantages of security and efficiency.

That is, as a server.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/E7/wKioL1dj332CH4Y8AAAu0ooFItw413.png "title=" 1.png " alt= "Wkiol1dj332ch4y8aaau0oofitw413.png"/>

The idea of Git is that it manages file modifications rather than the file itself, so the topic should be expanded around how to store and manage file modifications.

Next, with Git, let's see how it's managed.

    1. Install git

      sudo apt-get install Git-core

    2. Set user


    3.  git config --global user.name  "Your name" 
    4. $ git config --global user.email  "[email protected]" 

Note that the parameters of the git config command, --global using this parameter, means that all of your git repositories on this machine will use this configuration, of course, you can also assign a different user name and email address to a warehouse.

3. Create a version library

Repository also known as the Warehouse, English name repository, you can easily understand a directory, all the files in this directory can be managed by git, each file modification, deletion, git can track, so that any moment can be traced to the history, or at some point in the future can be "restored." Create a new folder, this directory will be your local repository, this repository can be empty, you can also make a directory you used previously.

through git init command to turn this directory into a repository that git can manage:

Next you will find a more. git hidden file in your folder.

Here's what the. git contains.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/E7/wKioL1dj5ByBQnrtAABsQ3bOAwo452.png "title=" QQ picture 20160617195020.png "alt=" Wkiol1dj5bybqnrtaabsq3boawo452.png "/>

This directory, except for the. git directory, is counted as a workspace.

The objects directory holds the actual files, and when the git add command executes, the files are stored in the objects directory.

Objects in the. Git/objects directory have a 40-bit ID, the first two bits as the directory name, and the last 38 bits as the file name

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/82/E8/wKiom1dj5cXggt20AABsQ3bOAwo476.png "style=" float: none; "title=" QQ picture 20160617195020.png "alt=" Wkiom1dj5cxggt20aabsq3boawo476.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/E7/wKioL1dj5bmAVcaNAAAoWFS3mHs360.png "style=" float: none; "title=" 0.png "alt=" Wkiol1dj5bmavcanaaaowfs3mhs360.png "/>

2.index is an index file. The information for the entire directory tree of staging area is stored, and the timestamp and length are saved for each file in the directory tree.

When git add adds a file to the staging area, the index file adds basic information about the file.

When we modify a file in the workspace (such as using touch config.js to modify timestamp information), this causes the timestamp of the file to change, and the old information is still stored in index.

At this point when we execute the GIT status command to check the status of the repository:

A. Git status is compared with the timestamp and length of the config.js timestamp and the length of the config.js stored in the index file.

B. If the same, no change is considered.

C. If the difference is found. Git status will continue to be compared with the contents of the Config.js file and the old version of Config.js (saved in. git/objects).

D. If the content does not change, simply update the latest config.js timestamp information to the index file

E. If the content changes, the content changes. However, the timestamp information for the config.js in the index file is not updated.

Because the algorithm first compares the timestamp and the length, avoids the comparison of the file contents at the same time, so the performance is relatively high.


So, the git add command does two things:

A. Adding files to Staging area (. git/objects)

B. Add a file index to (. git/index) 3. When Git commit is executed successfully, a sequence of objects representing the result of the commit is stored in the. git/objects directory. Mainly divided into three parts:

The A.tree object represents the staging area tree of the current commit, and the contents of the tree object are from the. git/index file.

B.blob Object

Files in tree object trees are always saved as blobs of objects. If there are three files in the tree, then there are three BLOB objects, all of which can be found through the summary information (ID, file name, type, etc.) of these BLOB objects saved by the tree object.

C.commit Object

This object records which tree object belonged to, the object ID of the last commit, its ID, author, and so on.

Therefore, when the Git commit operation executes, it will:

A. Create a tree object using the directory trees saved with the. git/index file.

B. As a result, the tree object naturally points to a blob file that has been added to the. git/objects by git Add.

C.. git/refs/heads/master file saves the ID of this commit

The 4.HEAD file holds the current branch, such as the contents of my head file:

[Plain] View Plain Copy Print ?

    1. [Email protected]:~/work/189/appengine/.git$ cat HEAD

    2. Ref:refs/heads/master

That is, head points to the master branch, and head can point to other branches if there are multiple branches.

5.. git/refs directory referred to as reference directory

A reference is a file that contains a commit ID: git/refs All references are saved.

A branch reference is saved in the. git/refs/heads directory, such as the./git/refs/heads/master file, which is a reference to the Master branch.

The. Git/refs/tags directory holds the tag reference.



This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1790431

What is git? Internal anatomy

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.