規則引擎 設計 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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/396316.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396316.shtml
英文地址,請注明出處:http://en.pswp.cn/news/396316.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

練習題之死鎖

public class PrintMain {public static String obj1"obj1";public static String obj2"obj2";public static void main(String[] args) {new Thread(new Runnable() {public void run() {System.out.println(new Date().toString "LockA開始執行&qu…

啟用或禁用對 Exchange Server 中的郵箱的 POP3 或 IMAP4 訪問

https://docs.microsoft.com/zh-cn/Exchange/clients/pop3-and-imap4/configure-mailbox-access?viewexchserver-2019 記錄下轉載于:https://www.cnblogs.com/amoy9812/p/9875426.html

java有什么壓力_編程語言的心智負擔!你學編程得有多大的壓力快來測試一下...

很多編程語言對比的文章,總喜歡比較各種編程語言的性能、語法、IO模型。本文將從心智負擔這個角度去比較下不同的編程語言和技術。內存越界如:C語言、C(C with class)C/C可以直接操作內存,但編程必須要面對內存越界問題。發生內存越界后&…

什么叫有效物理網卡_如何區分虛擬網卡和物理網卡?-阿里云開發者社區

一、什么是物理網卡和虛擬網卡?圖示如下:紅色部分包含VMWare的為虛擬網卡。通常,我們部署VMWare虛擬機、VMSphere虛擬集群、XenCenter虛擬集群是都會涉及虛擬網卡。二、辨別物理網卡和虛擬網卡的應用場景場景一:一般部署虛擬集群的…

算法復雜度的表示法_用簡單的英語算法:時間復雜度和Big-O表示法

算法復雜度的表示法by Michael Olorunnisola通過Michael Olorunnisola 用簡單的英語算法:時間復雜度和Big-O表示法 (Algorithms in plain English: time complexity and Big-O notation) Every good developer has time on their mind. They want to give their us…

Android Studio 開始運行錯誤

/********************************************************************************* Android Studio 開始運行錯誤* 說明:* 打開Android Studio就拋出這個錯誤。* * 2017-4-1 深圳 南…

IOS 計步器

這篇博客介紹的是當前比較流行的“計步器”-只是簡單的知識點 計步器的實現在IOS8開始進行了改變。 但是我會對之前之后的都進行簡單介紹。 IOS 8 - // // ViewController.m // CX 計步器 // // Created by ma c on 16/4/12. // Copyright © 2016年 bjsxt. All rights…

vue學習之二ECMAScript6標準

一、ECMAScript6標準簡述 ECMAScript 6.0(以下簡稱 ES6)是 JavaScript 語言的下一代標準,已經在 2015 年 6 月正式發布了。它的目標,是使得 JavaScript 語言可以用來編寫復雜的大型應用程序,成為企業級開發語言。 1.1E…

抖音吸粉_抖音吸粉5大實用方法首次分享!輕松實現粉絲10000+

抖音,是一款可以拍短視頻的音樂創意短視頻社交軟件,該軟件于2016年9月上線,是一個專注年輕人音樂短視頻社區。用戶可以通過這款軟件選擇歌曲,拍攝音樂短視頻,形成自己的作品。抖音APP僅推出半年,用戶量就突…

mapper mysql 主鍵_實現通用mapper主鍵策略兼容mysql和oracle

【原創文章,轉載請注明原文章地址,謝謝!】1.直接用官方提供的注解方法是無法達到兼容效果的2.跟蹤源碼看看是否有其他方法3.這里有個genSql,可以看一下這個類4.創建一個自定義的處理類實現GenSql(代碼中是我實際項目中用到的策略&…

權限分配界面 純手工 僅用到bootstrap的架構 以及 c標簽

<div class"form-group"> <div class"row"> <label class"col-sm-2 control-label">配置權限</label> <div class"col-sm-10"> <c:forEach var"m" items…

數據管理與數據庫 大學課程_根據數據顯示的50種最佳免費在線大學課程

數據管理與數據庫 大學課程When I launched Class Central back in November 2011, there were around 18 or so free online courses, and almost all of them were from Stanford.當我在2011年11月推出Class Central時&#xff0c;大約有18項免費在線課程&#xff0c;幾乎所有…

每天一個linux命令(12):more命令

more命令&#xff0c;功能類似 cat &#xff0c;cat命令是整個文件的內容從上到下顯示在屏幕上。 more會以一頁一頁的顯示方便使用者逐頁閱讀&#xff0c;而最基本的指令就是按空白鍵&#xff08;space&#xff09;就往下一頁顯示&#xff0c;按 b 鍵就會往回&#xff08;back&…

java 面試題 由淺入深_面試官由淺入深的面試套路

閱讀文本大概需要3分鐘。從上圖看來面試官面試是有套路的&#xff0c;一不小心就一直被套路。0x01&#xff1a;Thread面試官&#xff1a;創建線程有哪幾種方式&#xff1f;應聘者&#xff1a;繼承Thread類、實現Runable接口、使用j.u.c中的線程池面試官&#xff1a;繼承Thread類…

怎么用centos7運行c語言程序_centos如何編譯c語言代碼

centos如何編譯c語言代碼,文件,選項,作用,鏈接,程序 centos如何編譯c語言代碼 易采站長站,站長之家為您整理了centos如何編譯c語言代碼的相關內容。 編譯c,c++代碼 安裝gcc 1、使用如下命令查詢 centos 官方gcc的所有包:yum -list gcc* 可安裝的軟件包gcc.x86_64gcc-c++.x86…

第四篇:基本數據類型及用法(1)

字符串&#xff08;str型&#xff09; -可以做加法&#xff0c;乘法 乘法例&#xff1a; n1"alex" n2n1*3 print(n2) #結果&#xff1a;alexalexalex -首字母大寫: capitalize() -所有字母變小寫: casefold()、lower() #casefold更牛&#xff0c;很多未知的對應關系也…

Android Studio 錯誤集

錯誤列表與解決方案: 1.Android studio Gradle project sync failed Android studio 構建項目出錯 Error:Unable to start the daemon process: could not reserve enough space for object heap.Please assign more memory to Gradle in the projects gradle.properties file.…

需求簡報_代碼簡報:我如何通過做自己喜歡的事情來獲得頂級技術實習

需求簡報Here are three stories we published this week that are worth your time:這是我們本周發布的三個值得您關注的故事&#xff1a; How I landed a top-tier tech internship by doing something I love: 7 minute read 我如何通過做自己喜歡的事情獲得一流的技術實習…

review what i studied `date` - 2017-3-31

在11 月份內&#xff0c;每天的早上6 點到12 點中&#xff0c;每隔2 小時執行一次/usr/bin/httpd.sh 怎么實現0 6-12/2 * 11 * /usr/bin/httpd.shNginx中的ip_hash是指講一個地址的請求永久分發至后端的一臺RealServer&#xff0c;直至這臺RealServer宕機Zabbix和Nagios的工作原…

java string轉long報錯_java.lang.Integer cannot be cast to java.lang.Long解決辦法

你好我是辰兮&#xff0c;本次是項目遇到的java.lang.Integer cannot be cast to java.lang.Long異常以及相對應的解決方案。文章目錄一、實戰問題用postman測試數據報錯&#xff0c;類型轉換異常&#xff01;如何將Integer類型轉換成長整形 &#xff1f;先轉成String型&#x…