This guide walks 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 code at a specific commit or at a specific time.
It is advised 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 specific date, you can use the calendar dropdown to see all the commits for that day and find the one you want.
You can also use the command line to find the commit hash.
Once you find the commit you want to restore, you can create a new branch in that commit. Let’s call this branch working-branch
.
git checkout -b working-branch <commit-hash>
this git
will create a new branch named command working-branch
Pointing to a specified commit and switches to that branch.
Next, you can push the new branch to a remote repository.
git push -f origin working-branch
Rollback to a specific commit
Now that you have a new branch with the code in a specific commit, you can update the master 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 that will permanently delete all code changes made since the commit you’re restoring.