Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: how to go back to a previous commit

Tags:

git

I am in this situation in master:

     --c1--c2--c3--c4

I am in c4, but I don't like it, I would like go back to c1 and work on that in master:

        ______________   
       /              \
    --c1--c2--c3--c4   c6

Please how do I achieve that?

Thanks! :-)

like image 823
Lisa Anne Avatar asked Mar 15 '26 05:03

Lisa Anne


2 Answers

Do this -

git checkout c1

Return to master branch

git checkout master

git checkout -, which will checkout the previous branch or commit that HEAD pointed at.

Let me know if it helps.

Thanks!

like image 116
SantanuMajumdar Avatar answered Mar 17 '26 01:03

SantanuMajumdar


An easy way i use to step backwards in a number of steps is git checkout HEAD~[number]

If i want to go back for 3 steps, you'll write git checkout HEAD~3 if you ignore the number then git will assume it's 1 step

Of course you can always just take the hash and checkout to that hash

git checkout ABC123

Note that you will become in a detached head state, meaning there's no branch pointing to this hash, so you either git reset to drop all the other changes and make this commit your latest, or git branch to create a new branch name to make sure your commits don't become unreachable.

like image 33
Mohammad AbuShady Avatar answered Mar 16 '26 23:03

Mohammad AbuShady



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!