How to Restore a GitHub Repository Back to a Particular Commit

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

Confession: I hate Git. Heresy, I know. But true nonetheless. And I suppose that’s what brings you here. You made a mistake. You pushed several commits to the MASTER branch of your GitHub repository, but have now have had a change of heart. You’ve decided that you’d like to go back to the good ol days, a few commits ago actually. That’s what you want, but Git doesn’t care.

Awful isn’t it!?!? All you want to do is something fairly straight forward, reset your project back to a previous point in time, and Git does not want to allow you to do this. Checkout a previous commit? No problem! Push this commit to MASTER? Absolutely not!

Well luckily, I found an excellent tutorial on YouTube tonight on how to do this. It isn’t super simple. It probably isn’t the way the professionals do it. It probably doesn’t follow “best practices.” However, it works:

// From "merge -s ours" on YouTube by Colten Jackson
// https://www.youtube.com/watch?v=HkTlVCMo6_k
$ git checkout 1234321
$ git merge -s ours master
#### that merge will leave you in detached head, use "git log" to get latest commit hash, shown here as 9876789 #####
$ git checkout master
$ git merge 9876789
$ git push origin master
 

topherPedersen