Question

How to undo the last local commit in Git?

Recently I was making some changes to a local git repository and I committed some changes and files that I should not have committed.

I did not run git push so the changes were only committed to my local Git repository.

So I decided to share how I reverted the last commit here with the community in case someone gets in the same situation.

So rather than wiping out your whole local repo and cloning a fresh copy, you could do the following:


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
January 8, 2020
Accepted Answer

Of course, as with everything, there are multiple solutions.

But what I would usually do in this case in order to undo my latest commit and then commit my new changes is the following.

  • Let’s say that you made some changes and you committed the changes:
  1. git commit -m "Committing the wrong changes"
  • After that if you run git log you will see the history of everything that has been committed to a repository.

  • To undo the last commit, just run the following:

  1. git reset --soft HEAD~1

The above command will reset back with 1 point.

Note: the above would undo your commit but it would keep your code changes if you would like to get rid of the changes as well you need to do a hard reset: git reset --hard HEAD~1

  • After that make your new changes

  • Once you are done with the changes run git add to add any of the files that you would like to be included in the next commit:

git add .
  • Then use git commit as normal to commit your new changes:
git commit -m "Your new commit message"
  • After that, you could again check your history by running:
git log

Here’s a screenshot of the process:

Git How to undo latest commit digitalocean

Another approach would be to use git revert COMMIT_ID instead.

Here is a quick video demo on how to do the above:

Hope that this helps! Regards, Bobby

When you spend a lot of time using git, you forget how careful you have to be with the git commits. Even if you are a beginner or an expert, I’m sure almost everyone has made changes to a local git repository and committed some changes and files that you didn’t want to commit.

Don’t worry, it happens to the best of us. In this post, you will learn how to undo those changes.

How to undo if you haven’t run git push

If you have not run git push so that the changes were only committed to your local Git repository. That means there are a few solutions.

Let’s say that you made some changes and you committed the changes:

git commit -m "Committing the wrong changes"

After that, if you run git log you will see the history of everything that has been committed to a repository. To undo the last commit, you just need to run the following command:

git reset --soft HEAD~1

The command above will reset back with 1 point. This means that it will undo your commit but it will keep your code changes.

So if you would like to get rid of the changes as well you just need to do a hard reset. In order to do a hard reset run the command below:

git reset --hard HEAD~1

After you have done this, make your new changes.

Once you are done with the changes run git add. This will add any of the files that you would like to be included in the next commit.

git add .

Then use the git commit command as normal to commit your new changes.

git commit -m "Your new commit message"

After that it’s always a good thing to check your history again by running:

git log

Another approach would be to use git revert COMMIT_ID instead.

Conclusion

Mistakes happen to everybody, so it’s a good thing to know how to fix your mistakes. Remembering the things in this post will be very helpful and will make your life much easier.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel