Reverting Git Commits

R

Procedure to roll back both local and remote changes

shikokuchuo
2021-08-12

Reverting Local Git Commits

You have made a commit.

You discover a mistake or something you left out straight after the commit.

git reset HEAD~

This is a soft reset. Your changes are preserved. The commit is removed from the record.

Make the additional changes you need. Add files. Commit.

Reverting Commits Pushed to Remote (e.g. Github)

Copy your folder to a backup location.

The following is a hard reset, which rolls back to the previous commit. Changes since that commit will be lost. Force push it to the remote.

git reset HEAD^ --hard
git push origin -f

Both local and remote should now be in sync at the previous commit. You may check with:

git status

If you have Github Actions that are triggered by commits, they will be triggered again despite this being a roll-back. So go and stop those runs if necessary.

Next, if you have another branch such as ‘gh-pages’ that builds automatically on each commit, roll back that branch as well so it keeps in sync. As this branch has been building on the remote, do a git pull to ensure that your local copy is up to date first before resetting.

git checkout gh-pages
git pull

git reset HEAD^ --hard
git push -f origin gh-pages

Check status. Switch back to ‘main’ branch (substitute whatever branch you were on).

git status
git checkout main

Copy back the files with changes you made previously from your backup location.

Make the additional changes you need. Add files. Commit.

Citation

For attribution, please cite this work as

shikokuchuo (2021, Aug. 12). shikokuchuo{net}: Reverting Git Commits. Retrieved from https://shikokuchuo.net/posts/13-reverting/

BibTeX citation

@misc{shikokuchuo2021reverting,
  author = {shikokuchuo, },
  title = {shikokuchuo{net}: Reverting Git Commits},
  url = {https://shikokuchuo.net/posts/13-reverting/},
  year = {2021}
}