In Git, Revert to a Previous Commit Locally without Losing Any of your Previous Commit History
Posted on June 8, 2016 in Command Line, Git by Matt Jennings

To revert to a previous Git commit locally without losing any of your previous commit history, follow the steps below:

  1. In Mac Terminal, or another shell, do the command below to view all previous commits:
    git log --oneline
  2. Then you will see output similar to:
    723cdc5 updated sass to hopefully fix home page slideshow
    2336f9a fixed small JS bug
    922d67e completed home page slideshow JS
  3. Find the commit number you want to revert back to, like:
    922d67e completed home page slideshow JS
  4. Then, do the command below:
    git revert --no-commit 922d67e..HEAD
  5. Add all of the reverted file changes to a commit via:
    git add -A
  6. Do a commit with a comment, like below:
    git commit -m "reverted to previous commit"
  7. Finally, assuming that your repo is connected to a remote GitHub or Bitbucket repo via a SSH key, do a push to the remote repo:
    git push

Leave a Reply

To Top ↑