• Home /
  • Blog /
  • How to undo last commit in Git

How to undo last commit in Git

There are a number of ways to undo changes you commit into git repository, it depends on what exactly you want to change or undo and how.

For example, the most common task is 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 index

The following will undo your commit, but keep all files in 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 index

You can do even more and not only undo last commit, but remove files from 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 loose all changes you made, you can hard reset it. In that case your commit will be "destroyed" and you will loose all files you commit before.

git reset --hard HEAD~1

Technically commits are not destroyed from git, 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 than git checkout -b new-branch-name SHA-you-destroyed. Now your can work with your commit in 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 happen with your local repository, or you want to do full reset for your local repository and match it to remote. It removes all your changes and pull latest changes from remote origin.

git reset --hard origin/master



Yaplex logo white text

Yaplex is an IT Consulting and .NET development company co-founded by .NET Developer Alex Shapovalov who lives in Mississauga and provides software development services in Toronto, Mississauga, and GTA.

Contact Information

Toronto, Canada
Phone: +1 647 328 3809