我正在使用 git 来管理我的项目。在暂存文件时,我对 git 的工作流程感到困惑。
如果我暂存文件并忘记提交,然后再次修改该文件和暂存文件并提交,我该如何回滚到第一个暂存状态?
最佳答案
如“Git Internals - Git Objects”中所述,每次更改文件内容并将其添加到缓存时,您都会执行以下操作:
git hash-object -w test.txt
将其存储到存储的对象中。
我刚刚为一个文件连续做了两次 git add
,然后是:
C:\Users\VonC\prog\go\src\github.com\VonC\asciidocgo\.git>gfind -mmin -10 -print
.
./index
./objects/4f
./objects/4f/76d586459ec6ffc42257bb4c61d5422051cb10
./objects/61
./objects/61/24401794e7fee613f5a56593a0a8a059b2627a
(注意 -mmin
参数仅查找自最近 10 分钟以来修改过的文件)
我本可以在每个 git add
之后列出那些相同的 sha1 :
git rev-parse :0:abstractNode_test.go
参见 git rev-parse
(和“What does git rev-parse
do?”):在索引中添加文件仅是第 0 阶段(第 1、2 和 3 阶段在 merge 期间用于记录,仍在索引、共同祖先、源和目标版本中)。
如果我存储这两个 blob 的内容:
C:\Users\VonC\prog\go\src\github.com\VonC\asciidocgo>
git cat-file -p 4f76d586459ec6ffc42257bb4c61d5422051cb10 > a
git cat-file -p 6124401794e7fee613f5a56593a0a8a059b2627a > b
(请注意我如何将 blob 路径的“4f/
”部分与路径的其余部分连接起来以获得完整的 sha1)
a
和 b
之间的差异确实给了我添加到同一文件索引中的额外位。
torek还提到 in the comments :
Another (I think easier) way:
git fsck --lost-found
.
Dangling blob objects wind up in.git/lost-found/other/
and are alreadycat-file -p-ed
.
你确实会找到你在最后一个最终提交之前所做的所有之前的 git add。
在我的例子中,只有一个出现了:
C:\Users\VonC\prog\go\src\github.com\VonC\asciidocgo\.git\lost-found\other>dir
Volume in drive C has no label.
Volume Serial Number is D866-48E1
Directory of C:\Users\VonC\prog\go\src\github.com\VonC\asciidocgo\.git\lost-found\other
25/01/2014 17:31 <DIR> .
25/01/2014 17:31 <DIR> ..
25/01/2014 17:31 16 873 4f76d586459ec6ffc42257bb4c61d5422051cb10
关于git - 在 git 中暂存暂存文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21348996/