Skip to main content

Reset to a previous commit

Posted in Development and Git

Have you ever made a bunch of commits before realising you’ve been working directly on the main branch? I certainly have…

If you’ve committed work on a branch that you shouldn’t be committing work to directly, don’t panic: there’s an easy way to put things right!

Let’s assume the work is good and you want to keep it, but:

  • it should have been committed on its own feature branch
  • you need to put main back the way it was before you made the mistake

Make a branch with your work

The first thing you need to do is create a branch with all your work on it, which’ll keep it safe:

git checkout -b feature-i-forgot-to-make-a-branch-for

You can then jump back to the branch you were on:

git checkout main

Starting again

Next we need to go back in time to the last commit before we started committing straight to the main branch.

It’s worth mentioning that what we want to do is different to rolling back to a previous commit for a quick look around, before jumping back to the ‘present day’ again, which I mention in my article on changing your Git history. Instead, we’re looking to move back to a commit and throw out all of the commits you added in error.

So we need to view the list of commits and grab the hash/ID of the one immediately before the work we did, so let’s have a look at the log:

git log --oneline

As long as you haven’t run a git fetch when more work has been committed to the remote by another team member, you should spot something like:

abc1234 (origin/main, main) This is the commit message

This is telling you where things were before you started work on the wrong branch, so it should be easy to pick out of the list.

If some work has been committed and you’ve run git fetch, you’ll still be able to find that commit; just work your way back from the top to the commit before your first erroneous commit.

Copy the hash for the commit and the next step is to tell git to go back to that commit on this branch and discard all the commits that were done after it. Remember, all that work we’re losing on this branch has been safely moved to your new feature-i-forgot-to-make-a-branch-for branch.

git reset --hard abc1234

Your main branch should now be exactly the way it was before you committed that work directly to it, so you can now submit a pull request using your new branch in the normal way.

Accessibility in your inbox

I send an accessibility-centric newsletter on the last day of every month, containing:

  • A roundup of the articles I’ve posted
  • A hot pick from my archives
  • Some interesting posts from around the web

I don’t collect any data on when, where or if people open the emails I send them. Your email will only be used to send you newsletters and will never be passed on. You can unsubscribe at any time.

More posts

Here are a couple more posts for you to enjoy. If that’s not enough, have a look at the full list.

  1. Avatars and alt text

    I really enjoyed Nicolas Steenhout’s recent article on Alt text for avatars or user photos. But there is a context where I would break his rule…

  2. Upgrading from iPhone 13 mini to 16 Pro

    I get a new phone every 3-ish years, give mine to my wife, and now she gives hers to our daughter. I got a 16 Pro this year! Here’s the skinny.