Undo the last commit but keep the changes

Description: You committed too early. Undo the last commit, but keep your changes ready in the staging area.

Hint:

  • HEAD~1 means one commit before HEAD.

Solution

Terminal output

$ git reset –soft HEAD~1
$ git log –oneline
2875ee0 (HEAD -> main) First
$ git status -s
M f.txt

How it works

  • The branch moves back one commit, and the commit “Second” is gone from the history.
  • M shows that the change is still staged, so you can finish your work and commit again.

Read Git Reset.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.