How Git Works

what is Git?
Git is a Distributed Revision Control System......

a persistent Map

  • meet SHA1
    keys and values
    every piece of content has its own SHA1.
How Git Works_第1张图片
image.png

these will be Git's key to store "Apple Pie" in the map.


image.png

到目前为止,我们可以把Git 理解为一张地图,使用key和value来存储内容。但是这个persistent体现在哪里?

  • persistent
How Git Works_第2张图片
image.png
  • first commit
How Git Works_第3张图片
image.png

This means Git doesn't yet know what to do with them.
To commit a file I have to put it in the staging area first. Whatever is in the staging area will get into the next commit.
we can add files to the staging area with the git add command.
we can use "git log" to look at the list of existing commits.

what is a commit?

How Git Works_第4张图片
image.png
How Git Works_第5张图片
image.png

注意,commit 里面有一个tree信息,这个tree是什么?
A tree is a directory stored in Git. The commit is pointing at the root directory of the project.That's what this tree is, the root of the project.

看看tree的SHA1值:

How Git Works_第6张图片
image.png
How Git Works_第7张图片
image.png
  • versioning


    How Git Works_第8张图片
    image.png

Most commits have a parent.The very first commit is an exception.

second commit:


How Git Works_第9张图片
image.png
  • Tags
    A tag is like a label for the current state of the project.
    Git 中有两种类型的tag: regular tags and annotated tags.
    Annotated tags are the ones that come with a message.To create an annotated tag,you could use the git tag.
image.png
How Git Works_第10张图片
image.png
  • Git Object
  1. Blobs
  2. Trees
  3. Commits
  4. Annotated Tags
  • What Git really is ?
How Git Works_第11张图片
image.png

Branches

  • what branches really are
    Git 一般把branch放在.git文件夹下的refs文件夹中。
    Branch is nothing else than a simple reference.
How Git Works_第12张图片
image.png
How Git Works_第13张图片
image.png
How Git Works_第14张图片
image.png
  • The Mechanics of the Current Branch
    how does git know which is our current branch?
How Git Works_第15张图片
image.png
How Git Works_第16张图片
image.png
How Git Works_第17张图片
image.png
How Git Works_第18张图片
image.png

Mastering is moving . HEAD is just coming along for the ride.

How Git Works_第19张图片
image.png

使用checkout时发生了两件事:

  1. The first thing is that Git changes HEAD to point at lisa.
  2. Git replaced the files and folders in our working area,the working directory,with the files and folders in this commit.
How Git Works_第20张图片
image.png

if we add a new commit ,then it will look like:

How Git Works_第21张图片
image.png

Remember that branches are just references to commits.

  • Let's Merge!
How Git Works_第22张图片
image.png
How Git Works_第23张图片
image.png

编辑完冲突文件后,使用git status时,发现这个文件并未被添加到暂存区。

How Git Works_第24张图片
image.png

merge的时候,在没有冲突的情况下,下面这个最后的步骤是Git自动完成的,但是由于我们存在冲突的情况,在我们解决完所有冲突之后,我们需要手动来执行commit。

How Git Works_第25张图片
image.png

这个时候其实不需要给出commit message,Git知道我们在解决冲突的过程中,所以它会自动给我们写上。

How Git Works_第26张图片
image.png

我们可以直接退出编辑。

image.png

before merge

How Git Works_第27张图片
image.png

after

How Git Works_第28张图片
image.png
  • Time Travel for Developers
How Git Works_第29张图片
image.png

Git的对象之间相互引用。比如,一个父子commit之前的引用,commit到tree的引用,tree和blob之间的引用。引用基本上看起来都是一样的,但是有两个不同的用处。

  • References between commits are used to track history
  • All the other references are used to track content.

我想说明的东西是,when I checkout something,Git doesn't care about history. It doesn't look at ways that commits connect to each other. It just cares about trees and blobs. So,if you looking towards from this commit here,then Git forgets about the link to the parent of the commit,and it looks at the tree in the commit and all the objects that can be reached from there. That is the entire state of the project at the time of the commit,a complete snapshot of every file,every folder.Git uses this information to replace the content of your working directory. That's how you travel back and forth in time with Git。It's the whole point of versioning.

How Git Works_第30张图片
image.png

Objects and References

How Git Works_第31张图片
image.png

Rules

  • the current branch tracks new commits
    so if you create a new commit by saying git commit or git merge,then the current branch moves to the new commit.
  • your working directory is updated automatically. when you move to a commit,for example,with git checkout,git replace the content of your working directory with the content that can be reached from that commit.
  • any commit,blob or tree that can not be reached from either a branch,head or a tag is considered dead and can be garbage collected.

你可能感兴趣的:(How Git Works)