113 views
How do you undo the last commit in Git while keeping the changes staged?
The correct answer is
git revert HEAD
git reset --soft HEAD
git reset HEAD~1
git undo HEAD~1
Subscribe to RubyCademy and get free access to all our courses, plus hundreds of fun Ruby cards, quizzes, guides, and tutorials!
The command git reset --soft HEAD~
is a powerful tool in Git used to undo the last commit while keeping the changes staged.
Let's break down how it works and why it achieves this specific outcome:
The git reset
command is used to reset the current HEAD to the specified state. There are different modes of reset: --soft
, --mixed
, and --hard
.
The --soft
option specifies the mode of reset. When used with git reset
, it resets the HEAD to the specified state while keeping the changes from the undone commits staged.
The HEAD~
notation refers to the commit one step before the current HEAD. So, HEAD~
represents the immediate parent commit of the current commit.
Now, let's put it all together:
When you execute git reset --soft HEAD~
, Git resets the HEAD pointer to the commit before the current one (HEAD~
) while keeping the changes from the undone commit staged.
In summary, git reset --soft HEAD~
is a useful command for undoing the last commit in Git while preserving the changes made in that commit, allowing you to make additional modifications or corrections before committing again.
Voilà!
Subscribe to RubyCademy and get free access to all our courses, plus hundreds of fun Ruby cards, quizzes, guides, and tutorials!
RubyCademy ©