5

I'd like some Git log command to list out the commits before some specific commit ID/hash in the current working branch.

E.g. Something like

git log --before <commit id>

But I haven't been able to find what the correct command to do this is.

Kotosif
  • 338
  • 3
  • 12

1 Answers1

4

Just give the commit hash as the ref to explore via log

git log <commitHash>

and it will output all history from that point, backwards, until initial commit.

Alternatively, if you need to exclude this specific commit itself, then refer to its parent with

git log <commitHash>^

Sidenote about your mention of "in the current working branch"

Branches are technically irrelevant here to tree traversal logic. This won't limit output to what's "in" the branch, largely because the common metaphor is quite bad : commits are just not "on" branches. Branches are to be seen as soft (and disposable) shortcuts to designate one commit in the tree.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • Is there an easy composed command where you can say something like `git log [commitHash - 5 commits, commitHash + 5 commits]`? – c0mr4t Aug 12 '22 at 12:43
  • 1
    No there's not, because ancestry goes only one way. The `[commitHash - 5 commits]` part is trivial, it would be `~5`, but since commit histories in git follow the principles of DAG (directed acyclic graphs), nothing in a commit is stored about its children, only its direct parent(s). By following parent(s)'s own ancestry you can go back until root commit(s), but not in the other direction. – Romain Valeri Aug 12 '22 at 12:51
  • @c0mr4t Of course, if you drop the "easy composed" part of your request... [this](https://stackoverflow.com/a/2264471/1057485) could be interesting. – Romain Valeri Aug 12 '22 at 12:58