Git, A dream machine time (b)

 Previous describes the role and benefits of Git herein, this will carefully talk about what Git in the end yes.

      1, in the end what is Git?

  • component

      Git directly enter the official website to download and install, https: //git-scm.com/downloads

      

      As shown in FIG Git directory, a directory outermost local workspace and repository.

      .Git opening the file, you can see the local repository of the kernel:

    

    Not quite understand why the official should git divided into three work areas, staging areas and local warehouses, personal opinion is: according to " local repository (staging area file library + + other) workspace + " may be divided like this more clearer.

  • Git Network

      Single Git is very simple, when the interlinkages between Git exist on different computers, or servers, on the formation of a network of Git, we will exchange and share information in this system, eventually pushing the project forward.

       2, Workspace

     Workspace is what we usually manipulate files, to complete the project area, as shown above Git-Test file is a java project. Objects stored in the work area can be extracted from local files in the repository, it can also create their own editing and has not submitted documents into the warehouse.

      In short workspace is like an exposed interface that allows developers to manipulate files, without having to be concerned about the whole Git specifically how it works.

      3, local and remote warehouse warehouse

  • Warehouse is what the hell?

     Warehouse, as the name suggests is used to store things, we can understand it as a database . The local git repository .git is this folder, and regional warehouses stored data files are objects in the directory file folder.

   When we scratch or submissions (git add / git commit) of, Git file will be stored and encapsulated into objects, automatically calculates a checksum corresponding to the file as a file

    

      如上图所示"2a+f23c59e2358154b473160beb6104b32168dfef"这个40位的字符串就是一个校验和,玩过MongoDB的同学也一定非常熟悉,我们每次存入数据的时候都会自动生成一长串字符串作为主键,其实和这个原理相同。

       为了方便收纳管理,git把校验和的前2位单独拿出来作为文件夹,后38位则作为文件名,校验和前两位碰巧相同的文件就会被放在同一个文件夹中。git的本质其实可以这么认为:这一长串字符串就是一个key,而我们真正add的文件就是value。使用这个key,我们可以通过"git cat-file -p+ key"指令非常方便的在仓库中提取出我们提交的文件内容。

  • 啥叫远程和本地?

       本地和远程是相对的概念。你一旦在自己电脑上安装了git并完成了初始化,就拥有了一个.git文件,可以理解为一个空的数据库。这样,如果你从另一个数据库里拉取东西,也就是从远程拉取东西,那个数据库也就叫做远程仓库。举个栗子,你可以在自己电脑上初始化两个仓库,一个仓库从另一个仓库拉东西,在那个时点,被拉的仓库就是远程仓库。这和它存在于本地还是在网络上无关。

      虽然远程和本地的具体实例并不确定,但在通常情况下,我们都是在github服务器上创建仓库作为远程仓库,而在本机上创建仓库作为本地仓库。

       4、暂存区

         暂存区(staging area),又叫做index区,其实就是.git目录下的index文件。

     当我们对文件进行修改后,会使用add指令暂存文件,这时工作区改动的文件数据就会被封装,并加入到本地仓库中, 同时git会把这个文件的校验和存入到index文件中。一旦工作区文件被加入了暂存区,它就被纳入了git的管理范围。

  • 为什么要设置暂存区?

      我认为有两个原因:

    • 提供跟踪机制

     git会根据index中存放的校验和和时间戳对工作区的文件进行实时校验比对,以提示开发人员那些尚未暂存的修改。

    • 提交前的准备

       就像照相一样,每次拍照之前你需要取景、调节光圈快门白平衡这一系列的准备工作,而暂存区就是用来准备和缓存这些操作的,直到最终做好了准备,按下快门(commit)。

       有了暂存区,git会把index文件中保存的零散是文件校验和组织起来,做成统一的文件目录,最终在你提交(commit)的时候向仓库中存入这个目录。这个过程就是准备快照的过程。

        5、总结

      综上所述,经过实际测试和追踪证明,本地操作时的Git为我们做的事情应该如下所示:

      (假设A项目由2个文件:文件1和文件2组成)

         step1:开发者修改文件1

        step2:开发者使用git add将修改后的文件1加入仓库(objects文件夹),同时生成校验和,存入暂存区(index文件)中。

          step3:开发者修改文件2

          step4:同step2

  step5:项目已经修改完毕,开发者使用commit指令,git会将文件1和文件2组合成项目A(生成快照),并计算校验和,把快照保存到仓库中。

         step6:最终我们提取项目时不需要再单独把文件1和2提取出来,直接使用校验和提取项目A即可。

Guess you like

Origin www.cnblogs.com/mrpour/p/11222438.html