A .git directory, and understand the powerful Git!

Original: https: //www.daolf.com/posts/git-series-part-1/
Author: Pierre de Wulf
Translator: obviously moon, Editor: Guo Rui
Source: CSDN (ID: CSDNnews)

Git is a powerful tool, but using them is not very friendly. If programmers can really take the time to understand the composition of Git, it will avoid a lot of unnecessary trouble.

The following is the translation:

Git is like a beginner do not understand the language of the local people came to a strange country - if you know where, where to go, then fine. Once you get lost, that big trouble.

There are many online learning basic commands Git article, but this article does not fall into this category article. Here I will attempt to provide a different learning ideas.

Beginners are generally afraid of Git, it is difficult not afraid. There is no doubt, Git is a powerful tool, but using them is not very friendly. Use Git to understand a lot of new concepts, will file as a command parameters and not very different meanings both as a parameter.

I believe that in order to overcome these difficulties, not only to learn the use of the Git commit and push. If we can really take the time to understand the composition of Git, it will avoid a lot of unnecessary trouble.

First, the study .git directory

Well, we start now.

When you go through  git init when you create a git repository, git will create .git directory. This directory contains all the information necessary for the normal operation can git. Bluntly put, if you do not want to continue to use git in the project, directly to retain only delete .git directory project files. But why do so that you can do?

Here is the way the first time you submit .git folder:

 ├── HEAD  
    ├── branches  
    ├── config  
    ├── description  
    ├── hooks  
    │ ├── pre-commit.sample  
    │ ├── pre-push.sample  
    │ └── ...  
    ├── info  
    │ └── exclude  
    ├── objects  
    │ ├── info  
    │ └── pack  
    └── refs  
     ├── heads  
     └── tags
  • HEAD

    Revisit later.

  • config (configuration)

    This file contains your warehouse configuration, such as a remote url, your mailbox and user names. Every time you use the console  git config... will have an impact on this.

  • description (description)

    For gitweb (github a predecessor) to use, display a description of the warehouse.

  • hooks (hook)

    This is an interesting feature. Git provides a set of scripts that can be run automatically at every stage of meaningful Git. These scripts are called front and rear hook may submit (commit), becomes group (rebase), pull (pull) operation run. Life script indicates that its execution time. As we can prepare pre-push as a hook, to check before pushing code.

  • info (information)

    You can not want to be recorded in the file management git .gitignore file. Exclude files means that do not want to share the file. For example, you do not want to share your custom configuration of IDE, add it to the .gitignore file can be.

Second, the submission What is included?

Every time you create a document, and track it, git all files will be compressed and stored in its own data structure. The compressed object has a unique name and a hash value, and stores the objects (object) directory.

Before the study directory, we must understand the meaning of what once submitted Yes. You might say, it is to commit a snapshot of the current working directory, but it is much more than that.

In fact, when you commit, git through the following two steps to create a snapshot of your working directory:

  • If the file nothing changes, git just the compressed file (hash value) added to the snapshot.

  • If the file has changed, git will compress and store them in a folder object. Eventually, the name (hash values) of this compressed file to the snapshot.

Here is a simplified process, in fact, the whole process is a bit complicated, detailed description will be given in a future article. Focus on micro-channel public number: Java technology stack in the background reply: git, I can sort of get the latest Git N tutorial articles are dry.

Once the snapshot is created, it will be compressed to a hash value name. So where these compressed objects exist? They were the presence of object folder.

├── 4c  
│ └── f44f1e3fe4fb7f8aa42138c324f63f5ac85828 // hash  
├── 86  
│ └── 550c31847e518e1927f95991c949fc14efc711 // hash  
├── e6  
│ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391 // hash  
├── info // let's ignore that  
└── pack // let's ignore that too

This is what I look like after creating an empty file 1.txt and submit the object folder. Please note that if your file hash value "4cf44f1e ...", git will be stored "4c" subdirectory, and name it "f44f1 ...". This trick, the  /objects number of directories is reduced to 255 or less.

You have to remember that the submission contains four parts:

  1. A snapshot of the working directory name (a hash value).

  2. A review / comment.

  3. Submitter information.

  4. Hash value submitted by the parent.

If we extract the documents submitted:

// 通过查看提交历史,你可以轻松地查询到提交的哈希值  
// 你都不需要复制完整的哈希值字符串,  
// 复制能够保证哈希值的唯一性的前面一段即可。  
git cat-file -p 4cf44f1e3fe4fb7f8aa42138c324f63f5ac85828  

Get the following:

tree 86550c31847e518e1927f95991c949fc14efc711  
author Pierre De Wulf <test\[@gmail.com\](mailto:[email protected])> 1455775173 -0500  
committer Pierre De Wulf <\[[email protected]\](mailto:[email protected])> 1455775173 -0500  
  
commit A  

As expected, we saw a snapshot of hash values, comments and information submitted by the author.

There are two very important things:

  • As expected, the hash snapshot "86550 ..." is also an object, you can find it in the target folder.

  • Because this is the first submission, so there is no hash value of the parent commit.

So exist in the snapshot is Shane?

git cat-file -p 86550c31847e518e1927f95991c949fc14efc711  
  
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file_1.txt

We find the last object previously stored, and only a snapshot of our target. It is a blob object, which is another point of knowledge, not discussed here.

Focus on micro-channel public number: Java technology stack in the background reply: git, I can sort of get the latest Git N tutorial articles are dry.

Third, branch, tag, HEAD are the same

You now have learned that all the contents of git can be obtained through the correct hash value. Let us now focus on HEAD. So what is HEAD?

cat HEAD  
ref: refs/heads/master  

HEAD is not a hash, HEAD can be interpreted as pointing to the top of the branch you are using a pointer. We next look refs / heads / master:

cat refs/heads/master  
4cf44f1e3fe4fb7f8aa42138c324f63f5ac85828

It looks familiar it? This is the first time we submitted the hash values ​​are the same. This indicates that pointer branch (branch) and label (tag) is just a pointer to submission. This means that even if you delete the branch and you want to remove the label, they still point to submit still there, but after deleting difficult to obtain these submissions more difficult. If you want to know more details, you can learn by git book.

Fourth, written in the last

So learn here, you should understand that git it is to submit your current working directory file "compression", and then stored along with other information to the target folder. If you are familiar enough to git, you'll know which files will be included in the submission in which files will not be submitted.

I said here is submitted, does not mean your work directory snapshots, but rather a snapshot of the file you want to submit. Prior to the actual implementation, git will store files where you want to submit? It will store them in the index file. However, we do not intend to study it in depth. If you are really interested, you can (https://github.com/git/git/blob/master/Documentation/technical/index-format.txt) depth study through here.

Thanks for reading! We hope that by reading this article, you can learn valuable content. Hopefully, this article can help you more easily use git.

I recommended to my blog to read more:

1. the Java the JVM, collections, multithreading, new series of tutorials

2. the Spring MVC, the Boot the Spring, the Spring series of tutorials Cloud

3. Maven, Git, the Eclipse, IDEA Intellij Tool Tutorial Series

4. the Java, the back-end architecture, Alibaba and other manufacturers face new questions

Life is beautiful, see tomorrow ~

Guess you like

Origin www.cnblogs.com/javastack/p/12521911.html