Nahar
Nahar

Reputation: 269

How to revert and go the previous stage before commit

I am new at version control system (git). I did a mistake and when I realize it happen its too late. what I did

(1) commit current work 
(2) git pull --rebase origin master

Then I go to a previous commit using

(3) git checkout 858223adcac47288c44c4c07fbc3938773d778b8

Now I need to go the previous stage(1) before committed. And I really don't know how to go there. That's mean I want to go my master and undo the rebase.
I heartily thank if anyone tell me how to do this.

Upvotes: 0

Views: 1107

Answers (1)

dav23r
dav23r

Reputation: 86

git saves history of refs (read branches) mostly for this kind of scenarios when you want a commit that is no longer reachable from any of the current refs.

Unless enough time passed or you explicitly made reflog entries expire and garbage collect, the history is available by issuing git reflog <branch> outputting changes to branch to in form of <sha> <action description leading to changing value of branch> starting with most recent one.

Find the one you want to get back to (probably one that was prior to the commit you mentioned), examining:

git reflog master

Check it out to double check the contents:

git checkout <sha>

Move to master and reset master to that commit (caution that hard reset is potentially dangerous/troublesome if used incorrectly):

git checkout master

git reset --hard <sha>


Some notes:

How git-rebase works. This will help to understand how no longer reachable can arise during rebase https://git-scm.com/book/en/v2/Git-Branching-Rebasing

It's also possible to create another commit on top of rebased commit that effectively undoes the pulled part, but that's less practical and noted for completeness only.

Upvotes: 1

Related Questions