Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I git checkout one commit prior to a specific commit

Tags:

git

After going through github issues, I've found a commit that might be responsible for breaking code and I want to confirm this suspicion by doing something like:

git checkout --one-prior f1962b3cc771184a471e1350fa280d80d5fdd09d

like image 877
Milktrader Avatar asked Dec 08 '13 16:12

Milktrader


People also ask

How do you jump to a previous commit?

To jump back to a previous commit, first find the commit's hash using git log . This places you at commit 789abcd . You can now make new commits on top of this old commit without affecting the branch your head is on. Any changes can be made into a proper branch using either branch or checkout -b .


1 Answers

Here you go:

git checkout f1962b3cc771184a471e1350fa280d80d5fdd09d^

Notice the ^ at the end. That means one revision behind.

For example this would be 5 revisions behind:

git checkout f1962b3cc771184a471e1350fa280d80d5fdd09d^^^^^

... equivalent to:

git checkout f1962b3cc771184a471e1350fa280d80d5fdd09d~5

Btw, when you do this, you'll be in a detached HEAD state. The output explains this, which is very interesting and worth reading:

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

    git checkout -b new_branch_name
like image 185
janos Avatar answered Sep 30 '22 04:09

janos