How to undo the last commit in git

GitGit CommitGit ResetGit Revert

Git Problem Overview


By mistake, I did git add . and git commit in the develop branch. But luckily, I did not do git push.

So I wanted to revert it back to original state.

I tried git reset --soft and git reset HEAD --hard but looks like I have messed it up.

How do I fix this? I want to go back to original state and possibly keep the code changes.

Git Solutions


Solution 1 - Git

I think you haven't messed up yet. Try:

git reset HEAD^

This will bring the dir to state before you've made the commit, HEAD^ means the parent of the current commit (the one you don't want anymore), while keeping changes from it (unstaged).

Solution 2 - Git

Try simply to reset last commit using --soft flag

git reset --soft HEAD~1

Note :

For Windows, wrap the HEAD parts in quotes like git reset --soft "HEAD~1"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionchintan sView Question on Stackoverflow
Solution 1 - GitguessimtoolateView Answer on Stackoverflow
Solution 2 - GitAlways SunnyView Answer on Stackoverflow