Search code examples
git

How to display unreachable commits using `git log`?


I make use of the following git command to display the commit history below:

git log --graph --oneline --decorate --all -n 25

I want to display the (unreachable) commit 490ab89 as part of the history after resetting to the root commit 20513d9:

*   490ab89 (HEAD -> feature) Merge branch 'feature2' into feature
|\
| * a47187b (feature2) Adds random-file
* | b39cb83 (master) Adds some-file
|/
* 20513d9 Adds hello-world

git reset --hard 20513d9

* a47187b (feature2) Adds random-file
| * b39cb83 (master) Adds some-file
|/
* 20513d9 (HEAD -> feature) Adds hello-world

How can I make git log display unreachable commits?


Solution

  • You can give git log a raw commit hash ID, if you remember it:

    git log 490ab89
    

    or, with your options:

    git log --graph --oneline --decorate --all -n 25 490ab89
    

    Note that the -n 25 limit could conceivably hide 490ab89 if 25 other commits get displayed first. When git log has more than one commit to visit at a time—as it does with --all 490ab89 since the names feature, feature2, and master select three other commits, giving a total of 4 to visit initially—it places the commit hash IDs into a priority queue. It then takes one commit from the priority queue, displays it, and inserts its parent(s) into the queue. Commit 490ab89 will be visited only once it reaches the front of the queue, which obviously depends on its priority relative to other commits.

    (The default priority order is by committer timestamp, with later commits—those with higher timestamps—having greater priority. The --graph option implies the --topo-order option, which alters these priorities somewhat.)

    Assuming you have reflogs enabled, you can also use the reflog entries to locate the appropriate hash ID. In this case both HEAD@{1} and feature@{1} would name commit 490ab89.