I wanted to undo a commit, using IntelliJ I right-clicked on the last commit and then clicked "Revert Commit". 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). 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
.
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:
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:
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.