Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git how to restore old commit

I need to look at the commit i did few weeks ago e.g. when i do

> git log --oneline -10
b45e80d ten
711aa9c nine
166dbfa eight
26abb54 seven
ddd6bb6 siz
54430c3 five
ca2d76f four
81ccc8c three
d362fbc two
7d43aba one

i need to restore my site temporarily to state it was after i did this commit 81ccc8c three

How do i go about it?


Good thing i had repo pushed to bitbucket before i tried

git revert 81ccc8c

and my files did not update in project folder however it showed bunch of changes when i did git status and would not let me revert back to any other commit.

What should i do just go back to b45e80d ten

like image 435
John Smith Avatar asked Sep 21 '26 11:09

John Smith


2 Answers

You want to checkout that commit:

git checkout 81ccc8c

This will update your directory to reflect the contents of that commit. You may have to stash your changes first. Afterwards, you'll be in a detached head state, and you should avoid making any commits unless you associate some branch with your current commit.

like image 151
meagar Avatar answered Sep 24 '26 06:09

meagar


You can use checkout:

git checkout 81ccc8c

This will take you off your current branch and put you at the state of the commit that you use as parameter to checkout. To go back to your branch simply checkout the branch that you were on (e.g. master):

git checkout master

You don't want to use revert. That applies a new commit that undoes the commit that you pass as parameter to revert which isn't what you intended. To undo the effects of your unintended revert you should reset back to your 'ten' commit: git reset --hard b45e80d.

like image 44
CB Bailey Avatar answered Sep 24 '26 05:09

CB Bailey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!