Reverting Git Commits

[This article was first published on shikokuchuo{net}, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

                                                            sha256
1 010e16069f8858640c2bb9af4a2293b720161b71d9d6e1f375c30237ea2b4123

Reverting Local Git Commits

You have made a commit.

You discover an error / something you left out / something new to add 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.

To leave a comment for the author, please follow the link and comment on their blog: shikokuchuo{net}.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)