How to Rollback Changes to a Specific Commit in Github

This guide will walk you through the process of restoring your GitHub repository to a specific commit using Git commands.

Let’s say you have a Github repository where you push all your code changes. Each commit has a unique commit hash, and you can use this hash to restore the code to a specific commit or a particular time.

It is advisable that you take a backup of your current code before proceeding.

Find the Commit Hash

To get started, open your repository on Github and find the commit you want to restore. You can do this by clicking on the “Commits” tab and finding the commit in the list. If you want to restore to a particular date, you can use the calendar dropdown to see all the commits for that day and find the one you want.

Github Commit Hash

You may also use the command line to find the commit hash.

git log --oneline

Once you have found the commit you want to restore, you can create a new branch at that commit. Let’s call this branch working-branch.

git checkout -b working-branch <commit-hash>

This git command will create a new branch named working-branch pointing to the specified commit and switches to that branch.

Next, you can force push the new branch to the remote repository.

git push -f origin working-branch

Rollback to a Specific Commit

Now that you have a new branch with the code at the specific commit, you can update the main branch to this restored state.

git checkout main
git reset --hard working-branch
git push -f origin main

⚠️ Please be careful when using these git commands since it will permanently delete all code changes made after the commit you are restoring.

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻