我想在这里首先提到这个词index意思是一样的暂存区,并且你应该记住有three文件在任意时刻“活动”的版本:HEAD
版本、索引或“暂存”版本以及工作树版本。当你第一次跑步时git checkout <branch>
通常,所有三个版本都匹配。任何提交的版本都是永久的——就像提交一样永久——并且不可更改:您无法触及当前提交中存储的版本。你can随时覆盖索引和工作树版本,但正常模式是:
- 查看提交的版本:将提交复制到索引,然后将索引复制到工作树。
- 使用工作树版本。
- Use
git add
将工作树版本复制回索引。
重复步骤2、3直至满意;或使用git add --patch
建立一个类似于工作树版本但不同的索引版本。 (通常这样做是为了在运行调试文件时制作一些文件的可提交版本,其中没有额外的调试内容。)这确实意味着索引和工作树可以彼此不同and来自HEAD
commit.
如果你跑步的话git commit
,这会从索引/暂存区域中的任何内容进行提交就在那时。这就是为什么你必须保持git add
始终从工作树复制到索引中。
As Sajib Khan answered https://stackoverflow.com/a/47007365/1256452, git stash save
does make commits. More precisely, if git stash save
does anything (sometimes it does nothing, if there are no changes), it makes at least two commits. If you use the --untracked
or --all
flag, it makes three commits. The git stash documentation has a small diagram of this under its DISCUSSION section. https://www.kernel.org/pub/software/scm/git/docs/git-stash.html#_discussion Like the documentation, we'll mostly ignore the third commit.1
这些提交的不寻常之处在于它们是在no分支。特殊参考名称refs/stash
指向新创建的w
(工作树)提交。它至少有两个父母,其中一个是HEAD
提交,另一个是i
(索引)提交。和--untracked
or --all
有第三个父母(我称之为u
)保存额外的、未跟踪的文件。
In all but one case2 that we'll also ignore here, after saving away the index and work-tree versions of each file in the i
and w
commits, git stash save
then runs git reset --hard HEAD
to replace the index and work-tree versions of those files with the versions stored in the HEAD
commit. So your work is now saved, and can be restored later, but no longer exists in either index (aka staging area) or work-tree.
1If (and only if) you use the --all
or --untracked
option to create the third commit, Git also runs git clean
with appropriate options to delete the files stored in this third parent. Keep this in mind: any existing untracked files (whether ignored or not) are never included in either i
or w
. They are not saved at all, and hence not cleaned away either, unless you use these extra options. Note that the definition of an untracked file is simply any file that is not in the index right now