[git] How to undo the last commit in git

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.

This question is related to git git-commit git-reset git-revert

The answer is


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"


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to git-commit

How to undo the last commit in git git status (nothing to commit, working directory clean), however with changes commited Fix GitLab error: "you are not allowed to push code to protected branches on this project"? How to rollback everything to previous commit Troubleshooting misplaced .git directory (nothing to commit) Pushing empty commits to remote Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch How do I commit case-sensitive only filename changes in Git? How to save a git commit message from windows cmd? Remove files from Git commit

Examples related to git-reset

How to undo the last commit in git How can I move HEAD back to a previous location? (Detached head) & Undo commits What is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'? Git, How to reset origin/master to a commit? Can I delete a git commit but keep the changes? What is the meaning of git reset --hard origin/master? How to git reset --hard a subdirectory? git undo all uncommitted or unsaved changes Unstaged changes left after git reset --hard How do I use 'git reset --hard HEAD' to revert to a previous commit?

Examples related to git-revert

How to undo the last commit in git How can I move HEAD back to a previous location? (Detached head) & Undo commits How to undo local changes to a specific file How do I use 'git reset --hard HEAD' to revert to a previous commit? What's the difference between Git Revert, Checkout and Reset? Why does git revert complain about a missing -m option? How to revert uncommitted changes including files and folders? Reset all changes after last commit in git How do I revert a Git repository to a previous commit? Undo a particular commit in Git that's been pushed to remote repos