简体   繁体   English

Git 恢复已删除文件更改后的软重置

[英]Git soft reset following revert deleted files changes

I wanted to undo a commit, using IntelliJ I right-clicked on the last commit and then clicked "Revert Commit".我想撤消提交,使用 IntelliJ 我右键单击最后一次提交,然后单击“还原提交”。 A message told me no changes were made.一条消息告诉我没有进行任何更改。 By looking at the command log the command was:通过查看命令日志,命令是:

revert 75d72c1c0746b225d3857 --no-commit

The reverted changes appear in the changelist (or in the staged files area using sourcetree).还原的更改出现在更改列表中(或使用 sourcetree 在暂存文件区域中)。 I realized it wasn't the correct command to do what I needed, so I used:我意识到这不是执行我需要的正确命令,所以我使用了:

reset --soft HEAD^

The last commit was eliminated, but the edited files did not appear in the changelist.最后一次提交被删除,但编辑的文件没有出现在更改列表中。 It was as if I executed a reset --hard .就好像我执行了reset --hard

I managed to recover changes in another way, but I don't understand why the soft reset acted as a hard reset.我设法以另一种方式恢复了更改,但我不明白为什么软重置充当硬重置。

Let's use a modified version of the example that @0x5453 gave in a comment:让我们使用@0x5453 在评论中给出的示例的修改版本:

git init
echo "hello\n" >> foo
git add foo
git commit -m "add foo"
echo "goodbye\n" >> foo
git commit -am "modify foo"

At this point, the file foo has the following contents:此时,文件foo具有以下内容:

hello
goodbye

And we have two commits, one for each line in the file.我们有两个提交,一个用于文件中的每一行。

Now, when you do现在,当你这样做

git revert --no-commit HEAD

This "undoes" the changes in the most recent commit.这会“撤消”最近一次提交中的更改。 This appears as local changes that haven't been committed.这显示为尚未提交的本地更改。 If you do git status , you will see that foo is modified.如果你执行git status ,你会看到foo被修改了。 Its contents are now它的内容现在是

hello

which is the exact same as the first commit.这与第一次提交完全相同。

So now when you do所以现在当你这样做

git reset --soft HEAD~

You reset back to the first commit.您重置回第一次提交。 But since the contents of the file are exactly the same as that commit, you will not see any local changes.但是由于文件的内容与提交的内容完全相同,因此您不会看到任何本地更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM