如何使用更改恢复暂存文件,保留更改时间段的更改?

sta*_*low 10 git

说我编辑了一个文件

echo "hi" > someVersionedFile.txt

//Then I staged the file
git add .
git status

<console reads>
 Changes to be committed
 new file: someVersionedFile.txt 
Run Code Online (Sandbox Code Playgroud)

现在我对文件进行了其他更改

echo "hi again file" >> someVersionedFile.txt

//Then I restage the file with these changes
git add .
git status

<console reads>
 Changed but not commited
 modified file: someVersionedFile.txt
Run Code Online (Sandbox Code Playgroud)

问题:如何恢复上一个上一个版本?这可能是因为它没有被提交吗?

twa*_*erg 6

你可以这样做,但没有一个简单的命令可以帮你完成.

执行此操作时git add,该文件已添加到存储库中,但除了索引之外,目前没有任何指向它的文件.当您执行一秒时git add,新的(版本)文件将添加到存储库中,索引与文件的第一个版本之间的链接将替换为新链接.但第一个目标仍然存在.

您可以使用git fsck --dangling来获得你的资料库当前没有任何参考的BLOB对象的列表,但你必须通过他们一个去了一个带git show,git cat-file等等,以确定这是你想要的文件.找到合适的文件后,您可以git show HASH > somefile.txt(也许git add somefile.txt).

当然,如果你做任何git prune,git gc因为第二,等等git add,这实际上可能删除了您存储库中的原始文件.

这是一个简单的例子:

$ git init foo
Initialized empty Git repository in foo/.git/
$ cd foo
$ echo blah > foo.txt
$ git add foo.txt
$ echo blarg > foo.txt
$ git add foo.txt
$ git fsck --dangling
Checking object directories: 100% (256/256), done.
dangling blob 907b308167f0880fb2a5c0e1614bb0c7620f9dc3
$ git show 907b308167f0880fb2a5c0e1614bb0c7620f9dc3
blah
$
Run Code Online (Sandbox Code Playgroud)