97

I cloned a repository and want to switch to a commit to test my plugin against the core.

2 Answers 2

158

Instead of passing the name of a branch, you can pass any commit ID to checkout:

git checkout <commit-id>

See the man page.

5
  • Ah did it :) I thought that checkout only allows branch names.
    – xZise
    Mar 7, 2011 at 20:38
  • 3
    When you check out a non-branch object (a commit by its ID or a tag), you get a detached head. stackoverflow.com/questions/3965676
    – Rudi
    Mar 8, 2011 at 7:50
  • 12
    When you want to go back to the latest commit just use "git checkout nameOfYourBranch" Jan 7, 2013 at 11:00
  • Does commit ID means commit Hash here?
    – Mario
    Mar 2, 2014 at 1:51
  • @Mario: Yes. The man page changed in the meantime and just mentions "commit" now. Mar 2, 2014 at 1:58
21

Step 1: fetch list of commits:

git log

You'll get list like in this example:

[Comp:Folder User$ git log
commit 54b11d42e12dc6e9f070a8b5095a4492216d5320
Author: author <[email protected]>
Date:   Fri Jul 8 23:42:22 2016 +0300

This is last commit message

commit fd6cb176297acca4dbc69d15d6b7f78a2463482f
Author: author <[email protected]>
Date:   Fri Jun 24 20:20:24 2016 +0300

This is previous commit message

commit ab0de062136da650ffc27cfb57febac8efb84b8d
Author: author <[email protected]>
Date:   Thu Jun 23 00:41:55 2016 +0300

This is previous previous commit message
...

Step 2: copy needed commit hash and paste it for checkout:

git checkout fd6cb176297acca4dbc69d15d6b7f78a2463482f

Thats all.

4
  • 2
    With git log --pretty="%h - %s" the user can get a shortened hash (and the commit message) that also works on git checkout.
    – HRSE
    Feb 24, 2019 at 12:19
  • Doesn't work, result: fatal: your current branch 'master' does not have any commits yet
    – SPlatten
    Sep 7, 2020 at 12:43
  • You should add some commits to master at first.
    – Igor
    Sep 8, 2020 at 12:46
  • If it doesn't works, check the name of the commit again because VS code randomly edits what I paste with CTRL+V. The text I copied: e7a7f97076ca40ec9fe43f04eed79d824aea2665 The text I pasted: e7a7f97076ca40ec9fe43f04eed79d824aea26651~ Jan 22 at 16:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.