Git 中的 HEAD、工作树和索引之间有什么区别? [英] What's the difference between HEAD, working tree and index, in Git?

查看:35
本文介绍了Git 中的 HEAD、工作树和索引之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<小时>

编辑

我发现了这个

<块引用>

单个 git 存储库可以跟踪任意数量的分支,但您的工作树只与其中一个(当前"或签出"分支)相关联,并且 HEAD 指向该分支.

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

解决方案

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

  • <块引用>

    我使用索引作为检查点.

    <块引用>

    当我要进行可能会出错的更改时——当我想探索某个方向时,我不确定我是否可以坚持下去,甚至不确定它是否是一个好主意,例如在概念上要求很高的重构或更改表示类型 - 我将我的工作检查点到索引中.

    如果这是我自上次提交以来所做的第一次更改,那么我可以使用本地存储库作为检查点,但通常我会有一个概念上的更改,我将其作为一组小步骤来实现.
    我想在每一步之后进行检查点,但要保存提交,直到我重新开始工作、测试代码.

    <块引用>

    注意事项:

    1. 工作区是您看到和编辑的(源)文件的目录树.

    2. index/.git/index 中的一个单独的大二进制文件,它列出了当前分支中的所有文件,他们的sha1校验和、时间戳和文件名——它不是另一个包含文件副本的目录.

    3. 本地存储库 是一个隐藏目录 (.git),包括一个 objects 目录,其中包含该目录中每个文件的所有版本存储库(本地分支和远程分支的副本)作为压缩的blob";文件.

    不要将上图中表示的四个磁盘"视为 repo 文件的单独副本.

      • (注:如

        Can someone tell me the difference between HEAD, working tree and index, in Git?

        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.

        Does this mean that HEAD and working tree are always the same?

        解决方案

        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:

        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.

        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.

        (note: as commented by 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)

        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.

        这篇关于Git 中的 HEAD、工作树和索引之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆