Git: Rollback (or Undo) a Pull from an External Repository To Return To A Previous Stable Commit State
Zen is not possible and we need to return to a state of peace within Git!
In the case where one pulls from a repository such as GitHub and something breaks, one may want to undo that pull. Here are the steps to rollback to a previous version using Git.
Note if you have any work in the local working directory done after the pull, it will be lost using this method.
Our goal is to move to the Head to the last snapshot before the pull and return the Zen to us.
Steps
- Find the SHA-1 version using reflog. The reflog is interactive and one uses a q to exit out.
git reflog
That will bring up a list such as this:
da88c95 HEAD@{0}: pull origin br_1.0.0: Merge made by recursive. 26a96f1 HEAD@{1}: commit: Non movable end block not being placed at end time fix. abc8366 HEAD@{2}: commit: Created durationd dependancy to adjust the timeline correctly to the actual time. 1b3ca89 HEAD@{3}: commit: _ReSharper directory ignored 5d579ba HEAD@{4}: pull origin br_1.0.0: Fast-forward 68fbb98 HEAD@{5}: commit: Ignore tests which are creating timelines and provisioned timelines
We are interested in the second line as highlighted above which is before the pull.
- Then we want to move the head to either the hash or the symbolic name before the pull so we do a reset command which targets the local working directory and moves it to the target commit.
git reset --hard 26a96f1
or (in this case yours may be different)
git reset --hard HEAD@{1}:
The — hard options brings about changes to the working copy and sets us to the state before the pull. Now interestingly enough if we look at the log it shows our reset operation as the top level log item which has the same hash as where we wanted to go:
26a96f1 HEAD@{0}: 26a96f1: updating HEAD da88c95 HEAD@{1}: pull origin br_1.0.0: Merge made by recursive. 26a96f1 HEAD@{2}: commit: Non movable end block not being placed at end time fix. abc8366 HEAD@{3}: commit: Created durationd dependancy to adjust the timeline correctly to the actual time. 1b3ca89 HEAD@{4}: commit: _ReSharper directory ignored 5d579ba HEAD@{5}: pull origin br_1.0.0: Fast-forward
Final Thoughts
Of course you have only delayed the inevitable. One usually does this because something about the current state of changes in the remote is just not palatable and someone else is working on it. When ready do a normal pull to pull down those new changes and merge.