关于版本控制:在Git中,HEAD,工作树和索引之间有什么区别?

What's the difference between HEAD, working tree and index, in Git?

有人能告诉我头,工作树和索引的区别吗?

据我所知,它们都是不同分支的名称。我的假设正确吗?

编辑

我找到这个了

A single git repository can track an arbitrary number of branches, but your working tree is associated with just one of them (the"current" or"checked out" branch), and HEAD points to that branch.

这是否意味着头部和工作树总是相同的?


关于这些主题的其他一些很好的参考资料:

  • 我的Git工作流

氧化镁

I use the index as a checkpoint.

When I'm about to make a change that might go awry — when I want to explore some direction that I'm not sure if I can follow through on or even whether it's a good idea, such as a conceptually demanding refactoring or changing a representation type — I checkpoint my work into the index.
If this is the first change I've made since my last commit, then I can use the local repository as a checkpoint, but often I've got one conceptual change that I'm implementing as a set of little steps. I want to checkpoint after each step, but save the commit until I've gotten back to working, tested code.

Notes:

  • the workspace is the directory tree of (source) files that you see and edit.

  • The index is a single, large, binary file in /.git/index, which lists all files in the current branch, their sha1 checksums, time stamps and the file name -- it is not another directory with a copy of files in it.

  • The local repository is a hidden directory (.git) including an objects directory containing all versions of every file in the repo (local branches and copies of remote branches) as a compressed"blob" file.

  • Don't think of the four 'disks' represented in the image above as separate copies of the repo files.

    • 为什么Git比X好

    氧化镁

    • Git是你的朋友而不是敌人第3卷:参考和索引

    They are basically named references for Git commits. There are two major types of refs: tags and heads.

    • Tags are fixed references that mark a specific point in history, for example v2.6.29.
    • On the contrary, heads are always moved to reflect the current position of project development.

    氧化镁

    (注:正如Timo Huovinen所评论的那样,这些箭头不是提交所指向的,而是工作流顺序,基本上显示箭头为1 -> 2 -> 3 -> 4,其中1是第一个提交,4是最后一个提交)

    Now we know what is happening in the project.
    But to know what is happening right here, right now there is a special reference called HEAD. It serves two major purposes:

    • it tells Git which commit to take files from when you checkout, and
    • it tells Git where to put new commits when you commit.

    When you run git checkout ref it points HEAD to the ref you’ve designated and extracts files from it. When you run git commit it creates a new commit object, which becomes a child of current HEAD. Normally HEAD points to one of the heads, so everything works out just fine.

    氧化镁


    头(当前分支或当前分支上的最后提交状态)、索引(aka)之间的差异。临时区域)和工作树(签出中的文件状态)在Scott Chacon(Creative Commons Licensed)的Pro Git Book"1.3 Git基础知识"一章的"三个状态"部分中进行了描述。

    以下是本章中的图示:

    Local Operations - working directory vs. staging area (index) vs git repository (HEAD)

    在上图中,"工作目录"与"工作树"相同,"临时区域"是git"index"的备用名称,head指向当前签出的分支,tip指向"git目录(存储库)"中的最后一次提交。

    注意,git commit -a将分阶段进行更改,并在一个步骤中提交。


    您的工作树实际上是您当前正在处理的文件中的内容。

    HEAD是指向上次签出的分支或提交的指针,如果执行了新提交,则该分支或提交将成为新提交的父级。例如,如果您在master分支上,那么HEAD将指向master,当您提交时,新提交将是master所指向的修订的后代,并且master将更新为指向新提交。

    索引是准备新提交的临时区域。从本质上讲,索引的内容就是新提交的内容(不过,如果您执行git commit -a,这将在提交之前自动将Git知道的所有更改添加到索引中,因此它将提交工作树的当前内容)。git add将工作树中的文件添加或更新到索引中。


    工作树

    您的工作树是您当前正在处理的文件。

    Git指数

    • git"index"是放置要提交到git存储库的文件的位置。

    • 索引也称为缓存、目录缓存、当前目录缓存、临时区域、临时文件。

    • 在将文件"提交"(签入)到Git存储库之前,需要首先将文件放在Git"索引"中。

    • 索引不是工作目录:您可以键入一个命令,如git status,git将告诉您工作目录中的哪些文件已添加到git索引中(例如,使用git add filename命令)。

    • 索引不是git存储库:git索引中的文件是git在使用git commit命令时将提交给git存储库的文件。