You committed too early, or to the wrong branch. The files are fine. You want the commit gone and the changes back in your folder.
git reset --soft HEAD~1 removes the commit and leaves your changes staged, ready to commit again.
git reset HEAD~1 (the default, called mixed) removes the commit and leaves your changes in the files, not staged.
git reset --hard HEAD~1 removes the commit and deletes the changes from your files. Only use this if you mean it.
HEAD~1 means "one commit before the current one". Run git log --oneline first so you know which commit you are removing.
git reset --soft HEAD~1git status shows them staged (M f). Nothing lost.git reset HEAD~1 M f). Run git add to stage again. Nothing lost.git reset --hard HEAD~1you@laptop my-project % git log --oneline 7094b16 second 948aa49 first you@laptop my-project % git reset --soft HEAD~1 you@laptop my-project % git status -s M f you@laptop my-project % git log --oneline 948aa49 first
Your hashes will differ. The M in the first column means the change is staged. After a plain git reset HEAD~1 Git prints Unstaged changes after reset: and the M moves to the second column.
If other people may have pulled the commit, do not reset it. Use git revert HEAD --no-edit. It adds a new commit that cancels the old one, so everyone's history stays in line. If only you use the branch and you understand the risk, you can reset locally and then push with git push --force-with-lease. That rewrites the branch on GitHub, so pick it on purpose.
Only the message is wrong? You do not need a reset. git commit --amend -m "better message" replaces the last commit's message. Same rule: only before you share it.
Git remembers where HEAD has been. After a reset --hard you can look up the lost commit and go back:
you@laptop my-project % git reflog 948aa49 HEAD@{0}: reset: moving to HEAD~1 7094b16 HEAD@{1}: commit: second you@laptop my-project % git reset --hard HEAD@{1} HEAD is now at 7094b16 second
This only brings back committed work. Changes that were never committed are not in the reflog. The reflog also forgets old entries after a while, so act soon.
fatal: ambiguous argument 'HEAD~5': unknown revision or path not in the working tree.You went further back than your historyRun git log --oneline and count the commits first.fatal: not a git repository (or any of the parent directories): .gitYou are not in a repository folderSee the fix for this error.Run git reset --soft HEAD~1 to keep the changes staged, or git reset HEAD~1 to keep them in your files unstaged. Both remove the commit from the branch.
--soft moves the branch back and keeps your changes staged. Mixed (the default) also unstages them but keeps the files. --hard also resets the files, which deletes uncommitted changes.
Use git revert HEAD --no-edit, which adds a commit that cancels the old one and is safe on shared branches. Resetting and force pushing rewrites history for everyone who pulled it.
If the work was committed, find the commit with git reflog and run git reset --hard on its entry. Work that was never committed cannot be recovered this way.