How to undo last commit in Git
There are a number of ways to undo changes you commit into a git repository, it depends on what exactly you want to change or undo and how.
For example, the most common task is to change comment text for your commit, in this case, you can follow instructions from How to change last commit message in git
Undo your commit but keep changes in the index
The following will undo your commit, but keep all files in the index, so you can do
git commit again, and you will get the same commit.
git reset --soft HEAD~1
Undo your commit and remove your files from the index
You can do even more and not only undo the last commit, but remove files from the index as well, in that case, files still would be available on your machine, but if you want to do another commit, you need to add files to index first, using
git add command.
git reset HEAD~1
Completely undo your last commit and remove changes to files
If you don't care about your last commit and ready to lose all the changes you made, you can hard reset it. In that case, your commit will be "destroyed" and you will lose all files you commit before.
git reset --hard HEAD~1
Technically commits are not destroyed from git, a pointer to current commit just moved to another commit and commit you tried to destroy still will be available for something around 90 days on your local machine.
If you want to access it, type
git reflog and then
git checkout -b new-branch-name SHA-you-destroyed. Now you can work with your commit in the new branch and
git cherry-pick some commits or just ctrl + c and ctrl + v some of your changes.
Undo all local changes and reset to remote
The following command can be helpful if something happens with your local repository, or you want to do a full reset for your local repository and match it to the remote. It removes all your changes and pulls latest changes from remote origin.
git reset --hard origin/master