Is the commit you want to checkout in the ancestry of any of your branches? In other words, do any of your current branches include the changes made in the commit you're searching for?
If so, git log --all
will show this commit in the log. Adding the --oneline
and/or --decorate
flags might make this output easier to search through if all you're looking for is the commit message:
$ git log --all --oneline --decorate
If your commit is not in the ancestry of any of your branches (and therefore not shown with log --all
), then you have to search the git reflog
. The reflog is essentially a history of all of the commits that your HEAD
has pointed to. The HEAD
points to a commit when you have those files currently checked out in your working directory.
The format of the reflog is
hash HEAD@{n}: command: message
Where command
is the command issued to point HEAD
to this particular location, and message
is the message associated with this command. For commit
s, the message
is the commit message that you entered.
Because of this very well structured format, we can use grep to make the output easier to read through. For example, if you know that you've recently checked out the commit in question you can run:
$ git reflog | grep checkout
Or to only look through commits you can run:
$ git reflog | grep commit
You can also use grep in many other ways depending on what you're searching for:
$ git reflog | grep "phrase in commit message" --ignore-case
$ git reflog | grep "individual\|words\|in\|message" --ignore-case