“添加到索引”在 Git 中的真正含义是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25351450/
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 does 'adding to the index' really mean in Git?
提问by lapots
I've got a question regarding Git basics.
我有一个关于 Git 基础知识的问题。
Basically, what does the action known as "add to the index" mean in Git? I understand it like this:
基本上,Git 中称为“添加到索引”的操作是什么意思?我是这样理解的:
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.
如果对于任何文件 git 计算 SHA-1 总和,那么基本上添加到索引意味着它计算 SHA-1 总和并将文件添加到暂存区。
Am I correct?
我对么?
回答by jub0bs
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 areato indexbecause it lends itself to a useful metaphor. If committingis akin to "taking a snapshot", stagingis 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...
当然,如果您在拍完照片后立即意识到有太多孩子闭上了眼睛(或者有些孩子在给老师戴兔子耳朵!),您可能想放弃第一张照片并拍摄另一张更好的照片;在 Git 中,这将对应于修改最后一次提交。但我离题了...
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.
要进行某些操作,您通常会使用高级(“瓷器”)git add
命令...或完全等效的命令git stage
(由 Scott Chacon 在 Git v1.6 中引入),我发现它更直观,但看起来并不像受欢迎的。
When you add a new file to the staging area, three things happen:
当您将新文件添加到暂存区时,会发生三件事:
- the file contents are hashed,
- the file contents are stored in your repository's database,
- the file contents in your working tree are registered to the
.git/index
file.
- 文件内容被散列,
- 文件内容存储在您的存储库的数据库中,
- 工作树中的文件内容已注册到
.git/index
文件中。
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:
作为一个实验,为了修正想法,您可以使用低级(“管道”)Git 命令来重现在git add
这个简单情况下的作用。从一个全新的存储库开始:
$ cd ~/Desktop
$ mkdir teststage
$ cd teststage
$ git init
Before doing anything else, go ahead and peer into the .git/objects
folder.
在做任何其他事情之前,请继续查看.git/objects
文件夹。
$ ls -la .git/objects
You will see it only contains two (empty) subdirecties: info
and pack
. Create a file, say README.md
:
您将看到它只包含两个(空)子目录:info
和pack
. 创建一个文件,说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.
现在让我们开始README.md
,一步一步。首先,使用较低级别的git hash-object
命令来 (1) 散列内容README.md
和 (2) 将后者写入存储库的数据库。
$ git hash-object -w README.md
27728344ab3ae5b8aa334418d1e1b0f5be0ea0cc
(-w
means write, here.)
(-w
意思是写,在这里。)
Now, if you look into the .git/objects
folder, you will see that the new object (a blob) has been added to the database:
现在,如果您查看.git/objects
文件夹,您将看到新对象(一个 blob)已添加到数据库中:
$ 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
还剩下一件事要完成README.md
. 我们需要(3)将文件内容注册到索引中。看看里面.git
,里面应该还没有调用文件index
。现在,如果你跑
$ 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.
然后再看看里面.git
,你会看到index
已经创建了一个二进制文件。
That's it. You've staged README.md
. It's ready to go in your next commit. Check it for yourself:
就是这样。你已经上演了README.md
。它已准备好进入您的下一次提交。自己检查一下:
$ 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.
现在,如果您愿意,您可以进行第一次提交。
回答by Acey
When you add a file, it's marking that as a file that you will commit once you run the git commit command. A shortcut to add all modified files automatically is to commit with git commit -a. Another shortcut if you are adding multiple new files at the same time is to run git add -A.
当您添加文件时,它会将其标记为您将在运行 git commit 命令后提交的文件。自动添加所有修改过的文件的快捷方式是使用 git commit -a 提交。如果您同时添加多个新文件,另一个快捷方式是运行 git add -A。