4

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.

3
  • What do you mean by "the edited files did not appear in the changelist"? How are you viewing this "changelist"? Commented Feb 23, 2022 at 19:11
  • I'm using IntelliJ, it's the area where I see changed files. Now I noticed that after executing a revert --no-commit on the last commit, the reverted file appears in the changelist (or in the staged files area using sourcetree). Then if I execute the reset --soft in that state, the reset is applied as if it's reset --hard (changes are lost). Commented Feb 23, 2022 at 21:01
  • 1
    MCVE: git init; touch foo; git add foo; git commit -m "add foo"; echo "hello" >> foo; git commit -am "modify foo"; git revert --no-commit HEAD; git reset --soft HEAD~ Commented Feb 23, 2022 at 21:21

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So I understand that even if taken alone, neither revert nor restore -soft would cause you to lose the changes, taken together they do. This is because revert acts on the local copy of the files and restore acts on the repository.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.