規則引擎 設計 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.js和BlogController.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 clone或git 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