Git 中的 HEAD、工作树和索引之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3689838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between HEAD, working tree and index, in Git?
提问by Joyce Babu
Can someone tell me the difference between HEAD, working tree and index, in Git?
有人能告诉我在 Git 中 HEAD、工作树和索引之间的区别吗?
From what I understand, they are all names for different branches. Is my assumption correct?
据我了解,它们都是不同分支的名称。我的假设正确吗?
Edit
编辑
I found this
我找到了这个
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 存储库可以跟踪任意数量的分支,但您的工作树只与其中一个(“当前”或“签出”分支)相关联,并且 HEAD 指向该分支。
Does this mean that HEAD and working tree are always the same?
这是否意味着 HEAD 和工作树总是相同的?
回答by VonC
A few other good references on those topics:
关于这些主题的其他一些很好的参考资料:
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 workspaceis the directory tree of (source) files that you see and edit.
The indexis a single, large, binary file in
<baseOfRepo>/.git/index
, which lists all files in the current branch, their sha1checksums, time stamps and the file name -- it is not another directory with a copy of files in it.The local repositoryis a hidden directory (
.git
) including anobjects
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.
我使用索引作为检查点。
当我将要进行可能出错的更改时——当我想探索某个方向时,我不确定我是否可以坚持下去,甚至不确定它是否是一个好主意,例如在概念上要求重构或更改表示类型——我将我的工作检查点到索引中。如果这是我自上次提交以来所做的第一次更改,那么我可以使用本地存储库作为检查点,但通常我会有一个概念上的更改,我将其作为一组小步骤来实现。我想在每一步之后进行检查点,但保存提交,直到我重新开始工作,测试代码。
笔记:
该工作区是(源)的目录树中的文件,你看到和编辑。
该指数是一个单一的,大型的二进制文件
<baseOfRepo>/.git/index
,其中列出了当前分支中的所有文件,它们的SHA1校验和,时间戳和文件名-它不是在它的文件副本的其他目录。在本地资源库是一个隐藏目录(
.git
),包括objects
含回购每个文件的所有版本(本地分支机构和远程分支机构的副本)作为一种压缩“斑点”文件目录。不要将上图中表示的四个“磁盘”视为 repo 文件的单独副本。
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.
它们基本上是 Git 提交的命名引用。引用有两种主要类型:标签和头部。
- 标签是标记历史中特定点的固定引用,例如 v2.6.29。
- 相反,头部总是移动以反映项目开发的当前位置。
(note: as commentedby Timo Huovinen, those arrows are not what the commits point to, it's the workflow order, basically showing arrows as 1 -> 2 -> 3 -> 4
where 1
is the first commit and 4
is the last)
(注:如评论由蒂莫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 pointsHEAD
to the ref you've designated and extracts files from it. When you rungit commit
it creates a new commit object, which becomes a child of currentHEAD
. NormallyHEAD
points to one of the heads, so everything works out just fine.
现在我们知道项目中发生了什么。
但是要知道这里发生了什么,现在有一个称为 HEAD 的特殊参考。它有两个主要目的:
- 它告诉 Git 在您结帐时从哪个提交中获取文件,以及
- 它告诉 Git 在你提交时把新的提交放在哪里。
当您运行
git checkout ref
它时,它指向HEAD
您指定的 ref 并从中提取文件。当您运行时,git commit
它会创建一个新的提交对象,该对象成为 current 的子对象HEAD
。通常HEAD
指向其中一个头部,所以一切正常。
回答by Jakub Nar?bski
The difference between HEAD(current branch or last committed state on current branch), index(aka. staging area) and working tree(the state of filesin checkout) is described in "The Three States" section of the "1.3 Git Basics" chapter of Pro Gitbook by Scott Chacon (Creative Commons licensed).
HEAD(当前分支或当前分支上的最后提交状态)、索引(又名暂存区)和工作树(结帐中的文件状态)之间的区别在“1.3 Git 基础知识”的“三种状态”部分中有所描述Scott Chacon(知识共享许可)的Pro Git书的章节。
Here is the image illustrating it from this chapter:
这是本章中说明它的图像:
In the above image "working directory" is the same as "working tree", the "staging area" is an alternate name for git "index", and HEADpoints to currently checked out branch, which tip points to last commit in the "git directory (repository)"
上图中“工作目录”与“工作树”相同,“暂存区”是git“index”的替代名称,HEAD指向当前签出的分支,其中tip指向“ git 目录(存储库)”
Note that git commit -a
would stage changes and commit in one step.
请注意,这git commit -a
将在一个步骤中进行更改和提交。
回答by Brian Campbell
Your working treeis what is actually in the files that you are currently working on.
您的工作树实际上是您当前正在处理的文件中的内容。
HEAD
is a pointer to the branch or commit that you last checked out, and which will be the parent of a new commit if you make it. For instance, if you're on the master
branch, then HEAD
will point to master
, and when you commit, that new commit will be a descendent of the revision that master
pointed to, and master
will be updated to point to the new commit.
HEAD
是指向您上次签出的分支或提交的指针,如果您提交,它将成为新提交的父级。例如,如果您在master
分支上,HEAD
则将指向master
,并且当您提交时,新提交将是master
指向的修订的后代,并将master
更新为指向新提交。
The indexis a staging area where the new commit is prepared. Essentially, the contents of the index are what will go into the new commit (though if you do git commit -a
, this will automatically add all changes to files that Git knows about to the index before committing, so it will commit the current contents of your working tree). git add
will add or update files from the working tree into your index.
该指数是一个临时区域,其中新提交准备。本质上,索引的内容是将进入新提交的内容(尽管如果您这样做git commit -a
,这将在提交之前自动将 Git 知道的文件的所有更改添加到索引中,因此它将提交您的工作树的当前内容)。git add
会将工作树中的文件添加或更新到您的索引中。
回答by Ashraf Alam
Working tree
工作树
Your working tree are the files that you are currently working on.
您的工作树是您当前正在处理的文件。
Git index
Git索引
The git "index" is where you place files you want commit to the git repository.
The index is also known as cache, directory cache, current directory cache, staging area, staged files.
Before you "commit" (checkin) files to the git repository, you need to first place the files in the git "index".
The index is notthe working directory: you can type a command such as
git status
, and git will tell you what files in your working directory have been added to the git index (for example, by using thegit add filename
command).The index is not the git repository: files in the git index are files that git would commit to the git repository if you used the git commit command.
git“索引”是您将要提交到 git 存储库的文件的位置。
索引也称为缓存、目录缓存、当前目录缓存、暂存区、暂存文件。
在将文件“提交”(签入)到 git 存储库之前,您需要先将文件放在 git“索引”中。
索引不是工作目录:您可以键入诸如 之类的命令
git status
,git 会告诉您工作目录中的哪些文件已添加到 git 索引中(例如,通过使用该git add filename
命令)。索引不是 git 存储库:git 索引中的文件是如果您使用 git commit 命令 git 将提交到 git 存储库的文件。