Lisa Anne
Lisa Anne

Reputation: 4595

Git: how to go back to a previous commit

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! :-)

Upvotes: 12

Views: 8374

Answers (2)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42799

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.

Upvotes: 5

SantanuMajumdar
SantanuMajumdar

Reputation: 916

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!

Upvotes: 6

Related Questions