gpt4 book ai didi

git - Git 中的 HEAD、工作树和索引有什么区别?

转载 作者:IT王子 更新时间:2023-10-29 01:10:31 25 4
gpt4 key购买 nike

谁能告诉我 Git 中 HEAD、工作树和索引之间的区别?

据我了解,它们都是不同分支的名称。我的假设是否正确?

我发现了这个:

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.

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

最佳答案

关于这些主题的其他一些很好的引用:

workflow

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:

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

  2. The index is a single, large, binary file in <baseOfRepo>/.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.

  3. 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.

3 states

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.

commits

(注意:作为 commented by 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.

checkout

关于git - Git 中的 HEAD、工作树和索引有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3689838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com