Jacob Sharf
Jacob Sharf

Reputation: 250

git rev-list HEAD~n does not include first commit in range

I'm using git rev-list to get a selection of commits from a repo in two different ways:

git rev-list --reverse HEAD~<n>..

and

git rev-list --reverse <tag1>..<tag2>

Having read the git rev-list manpage, I know that <tag1>..<tag2> is equivalent to <tag2> ^<tag1>, and that this will not include <tag1> in the range of commits selected, however the man page didn't specify what to do if I wanted an inclusive range of commits (that is, including <tag1>)

I have the same problem with:

 git rev-list --reverse HEAD~<n>..

Here is a specification of what I want:

say I have four commits:

A--B--C--D

and I have a script which I give two commits:

myscript --from B --to D

I want a list of commits of size 3:

B, C, D

or if I did:

myscript --last 4

I'd get:

A, B, C, D

My proposed solution was to do something like:

git rev-list --reverse <tag1>~1..<tag2>

or

git rev-list --reverse HEAD~<n+1>..

however, this doesn't work if n == number of commits, or tag1 is the first commit.

Any help would be appreciated.

Upvotes: 2

Views: 2093

Answers (2)

Jeff Puckett
Jeff Puckett

Reputation: 40861

From the rev-list man page:

List commits that are reachable by following the parent links from the given commit(s), but exclude commits that are reachable from the one(s) given with a ^ in front of them.

To get an inclusive list of commits, just list the two references separated by a space. Your first reference should reference the previous commit with a tilda ~ and then be excluded which is denoted with a leading circumflex ^

git rev-list --reverse ^<tag1>~ <tag2>

For example:

git log --oneline
071f2c5 (HEAD -> master) D
8b2c3ee C
4f9aa7e B
6296bbd A
git rev-list --reverse ^4f9aa7e~ 071f2c5
4f9aa7e4d421e7cad3113bf92967cb646484ea25
8b2c3eeb38b21b5cf143e9a79c21bf23453eef85
071f2c527712d8d16aede35e1881a07fb55b408f

Upvotes: 0

Jacob Sharf
Jacob Sharf

Reputation: 250

So for the --last N option, I found that the following works quite nicely:

git rev-list --reverse HEAD -n <n> 

Unfortunately, the solution to selecting an inclusive range isn't as clean:

git rev-list --reverse <tag1>..<tag2> --boundary

this will give you what you want, however it will put a small dash ("-") in front of the first commit. This can be solved with a bit of string processing though.

Upvotes: 7

Related Questions