ulak blade
ulak blade

Reputation: 2665

How do I get hashes of newer commits than the current one in git?

in git I used gitk to see the hash of a commit that was made weeks ago and I used git checkout with that hash to get my source files to that previous state.

Now I want to go to a commit that is a few days later, however gitk only shows commits up until the current one, not later ones.

Using git log also shows only up until the current one.

How can I list later commits?

Upvotes: 2

Views: 879

Answers (3)

Zach Olivare
Zach Olivare

Reputation: 4161

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 commits, 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

Upvotes: 2

Chris Maes
Chris Maes

Reputation: 37842

you can use

git log --all --oneline --graph --decorate

to visualise your git tree. You are now in detached state; to return to where you were before; you need to use:

git checkout <branch-name>

where <branch-name> is the branch you were working on.

Upvotes: 0

Bustikiller
Bustikiller

Reputation: 2508

You can use git reflog to see the hash of all commits that have been visited by your HEAD pointer, that is, all the commits you have ever been pointing at in your working directory.

If you cannot get to the commit you are looking for using git log that means that the commit you are looking for is not an ancestor of the commit you are currently placed in. If you cannot find your commit in reflog, you may find a commit that was (directly or indirectly) based on it. If you find it, checkout to it, and the commit you look for should now be listed in git log.

Upvotes: 0

Related Questions