I am new to git, and I am running into one big issue. Namely how to revert a repository back to a good state if I do something wrong. I have seen how to use ‘git revert’ to make changes that undo previous changes, and I have read quite a few of the questions on stackoverflow and related locations, but I haven’t found a simple way to simply say “put my local repository bits back to exactly what they were before I did X” where X may be a commit, merge, or other repository operation.
Long Story:
This bit me particularly hard today when I was trying to setup a subtree branch for a third-party dependency (Jasmine) following the steps in the ProGit book on Subtree Merging. I followed the steps and setup a local repository with a branch that tracked a remote Jasmine repository. Then I connected this up using read-tree to a subdirectory in my probject and all was good. Unfortunately the Jasmine branch contained a submodule reference to a non-public URL and when I tried to fix this up I made the changes (and commits) on the branch I had tracking the remote repository. No matter what I did to try to revert these commits/changes, my ‘git push’ commands couldn’t work anymore because the repository thought I had changes to the Jasmine code and was trying to push them back through the public URL I was tracking.
Summary: I screwed up part of my respository and could not figure out how to get it back in the pristine condition it was in before I issued the commands caused by my git newbie status.
Is there anyway to rollback the entire repository a few commits/changes or do I just have to clone it again and start over when this happens?
Use git-reflog
Spot the revision that you want to go back to
(replacing n by the proper number)
Perhaps
git reset --hardafter that if you have unclean work tree; be very careful as those won’t be restoreable)