How to reset your main branch to a recently known working version

Here is a quick guide on how to reset your repository, main branch and live website to a previously proven working version without loosing changes since then.

First the CLI stuff:

cd your-website
git checkout main 
git checkout -b lastworking hashoflastworkingversion
git branch -m nonworkingchanges
git checkout lastworking
git branch -m main
git push origin --force
  • Step 1: go to your website folder
  • Step 2: check out the main/master branch (that’s the branch with the last changes that are not working)
  • Step 3: create a branch lastworking with the hash of the last change that worked on your site
  • Step 4: move the main branch to nonworkingchanges or another passive agressive branch name
  • Step 5: checkout lastworking
  • Step 6: set lastworking to main or master (we call them main these days because all branches matter)
  • Step 7: send the changes to the original repository (forcing the update like an, ehm, master)
  • Step 8 (not in the CLI output): pray that your upstream hoster can rebuild a working site (specifically on Netlify cancel all running deploys and “Clear cache and deploy” the site new)

After this you have two branches: main that contains the last known working version and nonworkingchanges that is x commits ahead of main, but not working.

Never change a running system. (Even if new features sound great.)

3 Likes

As is common with powerful tools there is more than one way of doing the same thing.

cd your-website
git switch main
git branch nonworkingchanges
git reset --hard hashoflastworkingversion
git push origin --force
  1. Go to your website folder
  2. Switch to the main branch, if you are not there already.
  3. Create a new branch “nonworkingchanges”, a copy of the main branch.
  4. Reset the main branch to the hash of the last change that worked on your site.
  5. Push the changes to the original repository. Using “force” to disable checks that normally not allow you to push changes that will lose commits

The “switch” command was introduced in git last year to take over some function from the overloaded “checkout” command.

3 Likes