在Git中'添加到索引'究竟意味着什么? [英] What does 'adding to the index' really mean in Git?

查看:642
本文介绍了在Git中'添加到索引'究竟意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基本上,在Git中称为添加到索引的操作是什么意思?我理解它是这样的:



如果对于任何文件git计算SHA-1总和,那么基本上添加到索引意味着它计算SHA-1总和并添加文件



我是否正确?

解决方案

有用的隐喻



将文件添加到索引,暂存文件,将文件添加到暂存区域等都是同义词。 / p>

我个人更喜欢术语临时区域索引,因为它适用于一个有用的比喻。如果提交类似于拍摄快照,则临时是关于撰写镜头



想象一下你自己是一个专业的摄影师,他们将拍摄一张照片:你收集所有的拍摄对象,准备照片,确保他们都在那里,没有入侵者,重要的是在框架中,等等......然后...... Snap!



修改上次提交 。但我离题...



当您将一个(新)文件添加到索引时会发生什么



你通常会使用高级(瓷器) git add 命令......或者完全相同的 git stage (由Scott Chacon在Git v1.6中介绍),我发现它更直观,但看起来并不像流行的那样。



当你添加一个新文件到临时区域,发生三件事:


  1. 文件内容被散列,

  2. 文件内容存储在版本库的数据库中,
  3. 将工作树中的文件内容注册到 .git / index 文件中。



使用管道命令将文件添加到索引中



作为为了解决这些问题,您可以使用低级(管道)Git命令重现 git add 在这种简单情况下所做的事情。从一个全新的存储库开始:

  $ cd〜/ Desktop 
$ mkdir teststage
$ cd teststage
$ git init

在做任何其他事情之前,先点击 .git / objects 文件夹。

  $ ls -la .git / objects 

您会看到它只包含两个(空的)子目录: info pack 。创建一个文件,比如 README.md

  $ printfhello \\\
> README.md

现在让我们将 README.md ,一次一步。首先,使用低级 git hash-object 命令来(1)散列 README.md 和(2)将后者写入存储库的数据库。

  $ git hash-object -w README.md 
27728344ab3ae5b8aa334418d1e1b0f5be0ea0cc

-w 表示写入,在这里。 )



现在,如果您查看 .git / objects 文件夹,您会看到新对象(一个blob)已被添加到数据库中:

  $ tree -la .git / objects / 
.git /对象
├──27
│└──728344ab3ae5b8aa334418d1e1b0f5be0ea0cc
├──info
└──pack

还有一件事是完成 README.md 的分期。我们需要(3)将文件内容注册到索引。看看 .git 里面,应该没有文件叫做 index 。现在,如果运行

  $ git update-index --add --info-only README.md 

然后再看看 .git ,你会看到一个二进制 index 文件已被创建。



就是这样。您已上演 README.md 。它已准备好进入下一次提交。检查它自己:

  $ git status 
分支主

初始提交

要提交的更改:
(使用git rm --cached< file> ...以取消存储)

新文件:README.md

现在您可以进行第一次提交了,如果需要的话。


I've got a question regarding Git basics.

Basically, what does the action known as "add to the index" mean in Git? I understand it like this:

If for any file git calculates SHA-1 sum then basically adding to index means that it calculates SHA-1 sum and add file to the staging area.

Am I correct?

解决方案

A useful metaphor

"Adding a file to the index", "staging a file", "adding a file to the staging area" are all synonymous.

I personally prefer the term staging area to index because it lends itself to a useful metaphor. If committing is akin to "taking a snapshot", staging is about "composing the shot".

Imagine yourself as a professional photographer about to take a a class picture: you gather all your subjects and get them ready for the photo, you make sure that they're all there and that there are no intruders, that everything of importance is in the frame, etc. Then... Snap!

Of course, if you realise, right after taking the picture, that too many kids had their eyes closed (or that some kid was giving the teacher bunny ears!), you may want to scrap that first picture and take another, better one; in Git, that would correspond to amending the last commit. But I digress...

What happens when you add a (new) file to the index

To stage something, you would usually use the high-level ("porcelain") git add command... or the exact equivalent git stage (introduced by Scott Chacon around Git v1.6) which I find much more intuitive, but doesn't seem nearly as popular.

When you add a new file to the staging area, three things happen:

  1. the file contents are hashed,
  2. the file contents are stored in your repository's database,
  3. the file contents in your working tree are registered to the .git/index file.

Adding a file to the index with plumbing commands

As an experiment, to fix ideas, you can use low-level ("plumbing") Git commands to reproduce what git add does in that simple case. Start from a brand new repository:

$ cd ~/Desktop
$ mkdir teststage
$ cd teststage
$ git init

Before doing anything else, go ahead and peer into the .git/objects folder.

$ ls -la .git/objects

You will see it only contains two (empty) subdirecties: info and pack. Create a file, say README.md:

$ printf "hello\n" > README.md

Now let's stage README.md, one step at a time. First, use the lower-level git hash-object command to (1) hash the contents of README.md and (2) write the latter to the repository's database.

$ git hash-object -w README.md
27728344ab3ae5b8aa334418d1e1b0f5be0ea0cc

(-w means write, here.)

Now, if you look into the .git/objects folder, you will see that the new object (a blob) has been added to the database:

$ tree -la .git/objects/
.git/objects
├── 27
│   └── 728344ab3ae5b8aa334418d1e1b0f5be0ea0cc
├── info
└── pack

There is one thing left to complete the staging of README.md. We need to (3) register the file contents to the index. Have a look inside .git, there should be no file called index in it, yet. Now, if you run

$ git update-index --add --info-only README.md

and then have another look inside .git, you'll see that a binary index file has been created.

That's it. You've staged README.md. It's ready to go in your next commit. Check it for yourself:

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   README.md

Now you can make your first commit, if you want.

这篇关于在Git中'添加到索引'究竟意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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