How to Reset to Previous Code State in Git / GitHub ⛰

Burning desert with cactus in foreground.

? WARNING ‼️ ? The method used here will erase Git history from both your local and remote repository. Use with caution.

This guide will tell you how to reset your code in Git to a previous state. To do this, we simply need to run two commands in the Git command line. These are:

git reset –hard [your commit’s hash here]

git push –force

Below I will explain in more detail.

Find the Commit’s Hash

We need to find the “hash” of the commit that we want to reset the codebase to. The hash is a unique code, automatically assigned to each commit. A Git hash is 40 characters long, but we don’t need the entire hash. Most likely, the commit’s hash will be unique with only 8 characters (unless your codebase is massive, in which case it may require up to 12, read more here). We can use as many characters as we want in the git push command that we will be using. If the hash is too short, Git will give an “ambiguous argument” error message, as shown below:

Git ambiguous argument error message.

Find the Commit’s Hash with GitHub Desktop

GitHub desktop allows us to easily see this short hash if we navigate to the commit in question. We need to click on the “History” tab at the top left area of GitHub desktop. From here, we should select the appropriate commit. At the top of the right-hand window, we will see the short hash for the commit (see images below).

GitHub desktop "History" tab.
GitHub desktop commit short hash.

Find the Commit’s Hash with the Command Line

To find the proper hash with the command line, we can use the command:

git log

This command lists our previous commits, and their respective hashes. We can use the “Enter” key to scroll down through our previous git commits. See screenshot below:

Git log

Reset Codebase and Push

Once we have the commit’s hash copied, we can use the command:

git reset --hard [your commit's hash here]

For example:

git reset --hard 02520a0

This command will reset your local codebase to its state at the selected commit. To push these changes to master (and erase all history up to that commit!) we use the command:

git push --force

Here is a screenshot of the Git flow in the command line:

Git reset - -hard command.

And that’s it! Your codebase should now be reset, and all history WIPED on both your local and remote repositories.

Extra Info: About the --force Flag

The --force flag makes Git push the change. By default Git doesn’t want to push this change because it can tell that history is going to be overwritten; the force flag makes Git push in spite of this. Without the --force flag, Git will reject the push, and present a rejection message, as shown below:

Git "updates rejected" error message.

Thanks for reading! I hope I didn’t accidentally make you erase a month’s worth of coding! ?

Burned forest.

Sources

StackOverflow post – How much of a Git sha is necessary to uniquely identify a change?

StackOverflow post – How to revert Git changes to previous state.

Article: The Git –force flag – How to use git push force the right way\

Photo Credits

Burning Desert by Frankie Lopez on Unsplash

Burned Forest by Anthony Gotter on Unsplash

Subscribe to new blog posts