规则引擎 设计 git_引擎盖下的Git

规则引擎 设计 git

by Wassim Chegham

由Wassim Chegham

引擎盖下的Git (Git under the hood)

Let’s explore some common Git commands, and dive into its internals to see what Git does when you run them.

让我们探索一些常见的Git命令,并深入了解其内部,看看运行它们时Git会做什么。

But first, let’s talk about Git itself.

但首先,让我们谈谈Git本身。

什么是Git? (What is Git?)

Put simply, Git is an open source distributed version control system. It was designed by Linus Torvalds, creator of the Linux kernel, to manage the source code of the kernel. So Git was designed from the start to be as fast and efficient as possible.

简而言之,Git是一个开源的分布式版本控制系统。 它是由Linux内核的创建者Linus Torvalds设计的,用于管理内核的源代码。 因此,Git从一开始就被设计为尽可能快速高效。

Git原则 (Git’s Principles)

In other version control systems such as CVS, Subversion, and ClearCase, the server is centralized — there’s a clear separation between the server and clients.

在其他版本控制系统(例如CVS,Subversion和ClearCase)中,服务器是集中式的-服务器与客户端之间存在明显的分隔。

When developers work on projects that use these systems, they first send a “checkout” request to the server, then retrieve a “snapshot” of the current version — usually the most recent one. Everyone has to go through the central server in order to work on the same project, sending “commits” or creating branches.

当开发人员在使用这些系统的项目上工作时,他们首先向服务器发送“签出”请求,然后检索当前版本(通常是最新版本)的“快照”。 每个人都必须经过中央服务器才能处理同一项目,发送“提交”或创建分支。

With Git, things are different. When you ask for a project, you clone it locally on to your machine.

使用Git,情况就不同了。 当您请求一个项目时,可以将其本地克隆到您的计算机上。

In other words, Git copies all the project files to your hard drive, then allows you to work on the project autonomously. All operations run locally on your machine. You don’t even need a network connection, except to synchronize with the source code by “pushing” or “pulling.”

换句话说,Git将所有项目文件复制到您的硬盘驱动器,然后允许您自主地处理项目。 所有操作都在计算机上本地运行。 您甚至不需要网络连接,只需通过“推”或“拉”与源代码同步即可。

That’s what makes Git so quick.

这就是让Git如此之快的原因。

With Git, you can:

使用Git,您可以:

  • “commit” your changes

    “提交”您的更改
  • change and create branches

    更改并创建分支
  • “merge” branches

    “合并”分支
  • retrieve a “diff” or apply a “patch”

    检索“差异”或应用“补丁”
  • recover different versions of any file

    恢复任何文件的不同版本
  • access the change history of any file

    访问任何文件的更改历史记录

And you can do all this without even being connected to Internet. Amazing, right?

而且,您无需连接到Internet就可以完成所有这些操作。 太好了吧?

工作流程示例 (An example workflow)

Let’s take a web application generated with Yeoman (don’t worry if you’re unfamiliar with this tool — it does not matter).

让我们看一下用Yeoman生成的Web应用程序(不用担心,如果您不熟悉此工具-没关系)。

Once Yeoman scaffolds out the application, creating its file tree structure, run git status. Git will respond that the current directory is not a Git repository:

一旦Yeoman搭建了应用程序,并创建了文件树结构,请运行git status。 Git会回答当前目录不是Git存储库:

So you need to run the git init command in the root directory in order to initialize a Git repository.

因此,您需要在根目录中运行git init命令以初始化Git存储库。

As you can see from the screen shot, we created an empty Git repository, and we are currently on its main branch — usually called “master.”

从屏幕快照中可以看到,我们创建了一个空的Git存储库,并且当前位于其主分支(通常称为“主”)上。

You can also notice that Git creates a .git folder at the root of the repo. This hidden directory is Git’s database. If you wish to make a backup of your project, simply make a tar.gz (or zip) of this directory.

您还可以注意到,Git在存储库的根目录下创建了一个.git文件夹。 这个隐藏的目录是Git的数据库。 如果要备份项目,只需在该目录下创建一个tar.gz (或zip )。

Let’s run git status to see our status:

让我们运行git status来查看我们的状态:

Git tells us that we haven’t added anything to our commit yet. So let us add the content of the current directory with the git add command:

Git告诉我们我们还没有添加任何内容。 因此,让我们使用git add命令添加当前目录的内容:

Before you commit your changes, you should check what you’re about to commit. To do that, run git diff:

在提交更改之前,您应该检查要提交的内容。 为此,运行git diff

Git tells us that we have changes waiting to be committed. Let’s commit them using the command git commit -m “first commit”:

Git告诉我们,我们有更改要等待提交。 让我们使用命令git commit -m“ first commit”提交它们:

Now let’s see how Git allows us to work on different features at the same time by using multiple branches.

现在,让我们看看Git如何允许我们通过使用多个分支同时处理不同的功能。

To illustrate this, we’ll open another terminal in the same directory, and run our application:

为了说明这一点,我们将在同一目录中打开另一个终端,然后运行我们的应用程序:

In order to create a new branch in Git, we use the verb “checkout” (with the -b flag):

为了在Git中创建一个新分支,我们使用动词“ checkout”(带有-b标志):

So we just created a new branch called “branch_A” and changed the current working context from the “master” branch to the “branch_A” branch.

因此,我们刚刚创建了一个名为“ branch_A”的新分支,并将当前的工作上下文从“ master”分支更改为“ branch_A”分支。

Any changes we make will only affect the current branch. Let’s make some changes to the home page of our application, such as changing the background color:

我们所做的任何更改只会影响当前分支。 让我们对应用程序的主页进行一些更改,例如更改背景色:

By running git status, we notice that we have some pending changes:

通过运行git status ,我们注意到我们有一些未完成的更改:

Let’s add this edited file and commit:

让我们添加这个编辑后的文件并提交:

Using the git branch command, we can see what branch we’re in. To go back to the “master” branch, we can type git checkout master.

使用git branch命令,我们可以看到我们所在的分支。回到“ master”分支,我们可以输入git checkout master。

After switching branches, we can see that— in the terminal where we launched our application — that the content of the file we modified in the branch “branch_A” has been reloaded and replaced by that of the branch “master” file:

切换分支后,我们可以看到-在启动应用程序的终端中-在分支“ branch_A”中修改的文件内容已被重新加载,并由分支“主”文件替换:

Please note that Git does not allow you to change your branch if you have pending changes. So if you really need to switch branches with pending changes, you can first ask Git to put aside those changes for you, using the git stash command:

请注意,如果您有未决的更改,Git不允许您更改分支。 因此,如果您确实需要使用待处理的更改来切换分支,则可以首先使用git stash命令让Git为您保留这些更改:

You then have the option of applying those changes later using:

然后,您可以选择以后使用以下选项来应用这些更改:

$ git stash apply stash@{0}

This will apply the first backup — because you specified {0}.

这将应用第一个备份-因为您指定了{0}。

When you’re working with several branches, at some point you will want to copy all the changes from one branch to another. Thankfully, Git has a git merge command that can do just that. Let’s merge branch_A into our current working branch, master:

当您使用多个分支时,有时需要将所有更改从一个分支复制到另一个分支。 幸运的是,Git有一个git merge命令可以做到这一点。 让我们将branch_A合并到当前的工作分支master中:

The good news: Git allows you to merge the same branch more than once. For example, imagine that you edit some files in branch_A, then merge them into “master.” Then you edit again branch_A again. Git does not prevent you from merging branch_A into master a second time.

好消息:Git允许您多次合并同一分支。 例如,假设您在branch_A中编辑了一些文件,然后将它们合并为“ master”。 然后,再次编辑branch_A。 Git不会阻止您第二次将branch_A合并到master。

现在让我们看看Git如何完成所有这些工作 (Now let’s look at how Git does all this)

Let’s assume that our project contains two files: BlogFactory.js and BlogController.js.

让我们假设我们的项目包含两个文件:BlogFactory.jsBlogController.js。

When we create our local repo with git clone or git init, Git initializes its database, and saves it in a hidden directory called .git:

当我们使用git clonegit init创建本地存储库时,Git会初始化其数据库,并将其保存在名为.git的隐藏目录中:

If we examine this folder, we see the presence of several subdirectories and files. The most interesting one are:

如果我们检查此文件夹,则会看到存在多个子目录和文件。 最有趣的是:

  • HEAD: this file contains the path to the reference that indicates the current branch

    HEAD :此文件包含指向当前分支的引用的路径

  • config: the repo configuration file

    config :仓库配置文件

  • objects: this is the directory that contains all the repo files, their content is encoded and compressed

    objects :这是包含所有回购文件的目录,其内容经过编码和压缩

  • refs / heads: this directory contains a file per branch. Each file is named after a branch, and its content is the SHA1 of the last commit (as explained below)

    refs / heads :此目录每个分支包含一个文件。 每个文件都以一个分支命名,其内容为最后一次提交的SHA1(如下所述)

When you create or edit files, you need to run:

创建或编辑文件时,需要运行:

$ git add BlogFactory.js BlogController.js

Or:

要么:

$ git add . # in order to add all unstagged files

This command tells Git that you want to add a snapshot of your files. So Git retrieves the current state of the contents of your files, then computes their checksums using SHA1, and creates an entry in its database. The key of this entry is the SHA1 hash, and its value is the raw contents of the file.

该命令告诉Git您要添加文件快照。 因此,Git检索文件内容的当前状态,然后使用SHA1计算其校验和,并在其数据库中创建一个条目。 此项的键是SHA1哈希,其值是文件的原始内容。

Yes, all the content!

是的,所有内容!

Then, as we said earlier, you need to commit these changes. To do that, you run the command:

然后,正如我们之前所说,您需要提交这些更改。 为此,运行命令:

$ git commit -m "A very useful commit message"

At this point, Git records a sort of “manifest” representing the whole file structure tree, with each filename and its SHA1 key in its database. Then it calculates the checksum of this manifest, based on its contents. Then it links to the new commit.

此时,Git记录了一种表示整个文件结构树的“清单”,其中每个文件名及其SHA1密钥都在其数据库中。 然后,它根据清单的内容计算清单的校验和。 然后,它链接到新的提交。

Now imagine that you have changed the BlogController.js file, and you redo a git add. Git performs the same process as before. It creates a new entry in its database, and because the file contents have changed, the SHA1 checksum has also changed.

现在,假设您已经更改了BlogController.js文件, 并重做git add。 Git执行与以前相同的过程。 它在数据库中创建一个新条目,并且由于文件内容已更改,因此SHA1校验和也已更改。

Then, when you do a git commit, Git recreates a new manifest with the new entry SHA1:

然后,当您执行git commit时 ,Git用新条目SHA1重新创建一个新清单:

Now suppose that you rename your file to MyBlogController.js for instance, and then commit your changes again. Git does not create a new entry in the database since the content—and the SHA1—have not changed:

现在,假设您将文件重命名为MyBlogController.js ,例如,然后再次提交更改。 Git不会在数据库中创建新条目,因为内容和SHA1均未更改:

Here’s what’s actually happening in the Git database:

这是Git数据库中实际发生的事情:

Git has stored the content of both committed files in the directory .git/ objects. Beside those committed files, Git also saves a file containing the details of the commit, and a manifest file as described above.

Git已将两个已提交文件的内容存储在目录.git / object中 。 除了这些提交的文件,Git还保存了一个包含提交细节的文件和一个清单文件,如上所述。

The Git command cat-file -p SHA1 is used to read the content of the stored objects. The SHA1 hash is composed of the first two bits of the directory objects/XX/ with another 38-bits forming the name of the object objects/XX/YY..YY. For example:

Git命令cat-file -p SHA1用于读取存储对象的内容。 SHA1哈希由目录对象/ XX /的前两位组成,另外38位构成对象对象/XX/YY..YY的名称。 例如:

$ git cat-file -p 987451acde8030ef93abaaff87daa617316cc7c7

You can also enter the first 8 bits of the SHA1 (those actually are enough):

您还可以输入SHA1的前8位(实际上已经足够):

$ git cat-file -p 987451ac

Similarly, the content of the object storing the information of the commit looks like this:

同样,存储提交信息的对象的内容如下所示:

As you can see, the commit object file contains some information related to that commit, including the SHA1 of manifest (tree), which looks like this:

如您所见,提交对象文件包含与该提交有关的一些信息,包括清单(树)的SHA1,如下所示:

So as you may have guessed, Git does not really care about file names. It cares more about their content. Even if you copy a file, Git will not create a new entry in its database. It’s just a matter of content and SHA1 hashes.

因此,您可能已经猜到了,Git并不真正在乎文件名。 它更关心他们的内容。 即使您复制文件,Git也不会在其数据库中创建新条目。 这只是内容和SHA1哈希值的问题。

And if you’re wondering: when I do a git push, what does Git really do? Well, Git computes the delta between the two files, compresses it, and then send it to the server. Git does not send the file’s entire content.

而且,如果您想知道:当我执行git push时, Git到底在做什么? 好吧,Git计算两个文件之间的增量,将其压缩,然后将其发送到服务器。 Git不会发送文件的全部内容。

翻译自: https://www.freecodecamp.org/news/git-internals-for-curious-developers-a1e44e7ecafe/

规则引擎 设计 git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值