How to Rollback Changes to a Specific Commit in Github

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.



source https://www.labnol.org/restore-github-commit-240904

Post a Comment

0 Comments