Git log range revision including the range edges
Asked Answered
B

2

11

I know that this type of questions is addressed in a lot of Q/A, I have studied them but actually I did not found a way to get the what I want.

I am looking for the git logs between two hashes including the logs of hashes used in the range.

From looking to the docs and other Q/A in SO I have found that it is achievable this way:

git log oldest...Newest

But this does not return the log of oldest is excluded.

For example in this repo https://github.com/galniv/jira-git-helper/commits/master

I want the logs between 52209d3738f80e49c724502503d6a72c1e24e6bc as the oldest and 5456f8cecc5ef5cef5abc1f820a4f044e2153734 as the newest.

The result of git log 52209d3738f80e49c724502503d6a72c1e24e6bc...5456f8cecc5ef5cef5abc1f820a4f044e2153734 returns only two logs instead of 3 :

commit 5456f8cecc5ef5cef5abc1f820a4f044e2153734
Author: Gal Niv <[email protected]>
Date:   Tue Mar 21 15:33:55 2017 -0400

    Update README.md

commit a4c184ecc7fb4c50705c0ac90c94752a31ce7251
Author: Gal Niv <[email protected]>
Date:   Tue Mar 21 11:48:30 2017 -0400

    Remove console.log, display current branch name

How to include the log of the oldest hash?

Backlash answered 21/3, 2017 at 20:27 Comment(0)
G
6

If there's no merge-commit involved, I'd use the ~:

git log old-rev~1..new-rev

That should be enough.

Gynecocracy answered 21/3, 2017 at 20:38 Comment(5)
Even if there are merge commits involved, it should work fine.Gynecocracy
But it fails if old-rev is the first commit, because there is no commit before it.Yttrium
As Willem here points out, this is not a generic solution.Avril
This is a good answer, but @McKay's is better.. as this will fail in cases where old-rev is the first commit.Bang
It does not work fine on merge commits. It only excludes one of the parents old-rev, so it will also the whole other branch that the commit was merging. You need @McKay's answer even if you are sure old-rev is not the initial commit.Norby
E
5

Use

git oldest^! newest

The two dot notation oldest..newest is just shorthand for ^oldest newest. In other words, those commits reachable from newest but not from oldest.

"The r1^! notation includes commit r1 but excludes all of its parents." (source). Putting these ideas together, oldest^! newest creates a range that includes both endpoints.

Running git rev-parse oldest^! helped me understand how this works.

Eulalie answered 16/6, 2022 at 0:8 Comment(1)
Do you mean git log oldest^! newest ?Womanize

© 2022 - 2024 — McMap. All rights reserved.