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:
- In Mac Terminal, or another shell, do the command below to view all previous commits:
git log --oneline
- 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
- Find the commit number you want to revert back to, like:
922d67e completed home page slideshow JS
- Then, do the command below:
git revert --no-commit 922d67e..HEAD
- Add all of the reverted file changes to a commit via:
git add -A
- Do a commit with a comment, like below:
git commit -m "reverted to previous commit"
- 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