223

Is there anyway to get a list of commits from a given commit number to HEAD?

I know this is possible by the commit date, but I need it by the commit number and I can't seem to find any documentation, or even if this is possible.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
ehftwelve
  • 2,787
  • 2
  • 20
  • 25
  • What Git command were you using to list commits from a given commit name? – Greg Hewgill Oct 07 '11 at 22:09
  • 1
    You do mean commit hash, right? You can also use `HEAD~10` to mean "10 commits before HEAD" so that you can do `git log HEAD~10..`, if that's what you mean by commit number. – Cascabel Oct 08 '11 at 00:06

8 Answers8

254
git rev-list <since_hash>..HEAD

or to include the commit:

git rev-list <since_hash>^..HEAD

You can use git log instead of git rev-list as well to get additional details.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • This worked perfectly for me, thanks! I swear that I had tried this. Must have made a mistake. – ehftwelve Oct 11 '11 at 19:37
  • 1
    @manojlds I know that `HEAD` is pointing to the lasted commits, but what is the meaning of `^` – Kasun Siyambalapitiya Dec 01 '16 at 09:14
  • 2
    @KasunSiyambalapitiya `^` is an alias for first parent. See "SPECIFYING REVISIONS" in https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html -- also see the manual https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection -- or http://stackoverflow.com/questions/2221658/whats-the-difference-between-head-and-head-in-git – Michaelangel007 Jan 20 '17 at 18:34
  • 3
    I get a huge, huge list of SHAs... even for a commit that's just one back (one leg of a merge). What is it telling me and how is that useful? – ErikE Feb 26 '17 at 01:30
  • If you just care about the *count* of commits, you can specify the `--count` option after `rev-list`. i.e.: `git rev-list --count ..HEAD` – Jon Schneider Feb 10 '20 at 02:47
  • What if I only need certain count of commits after as there'd have plenty commits from to HEAD? – SHUMING LU Dec 07 '22 at 06:15
116
git log <hash>..

Is the least amount of typing. Omitting "HEAD" assumes that's what you meant. Rev-list would work too.

user229044
  • 232,980
  • 40
  • 330
  • 338
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • 3
    This one is my favorite. I like to combine it with `--oneline`. Instead of ``, of course, you can use a ref like a branch name, too. – codener Jun 07 '17 at 06:34
  • 3
    Also `git log HEAD~#..` where `#` is the number of recent commits desired. – user3.1415927 Apr 30 '19 at 16:54
  • def the best and most concise option! – Ricardo Rodrigues Jul 10 '19 at 11:54
  • 1
    It works, however it should be mentioned it behaves weirdly, I almost deemed it doesn't work. E.g. if you execute a `git log --oneline 851c30c9badf^..` in Linux kernel tree, you'll find it does not start with `851c30c9badf`, it starts instead with the most recent commit. "Alright, the `851c30c9badf` is probably at the end" one might think. Well, it isn't, it's followed by hundreds of other commits. Also, you can't use a `-5` command to see just 5 commits since the one you're interested in, not even if you pass `--reverse` arg. So depending on repo this command might give too much output. – Hi-Angel Jun 08 '21 at 15:26
  • FYI you can go the opposite way (get commits _up to_ but excluding ) with `git log ^@` – jberryman Jun 21 '21 at 17:22
  • where `` can just as well be a branch name, so to see what changes have been made on top of `develop` for a feature branch, first `git fetch` then `git log origin/develop..origin/feature-branch-name` for example. No need to copy/paste those pesky hashes. – Ed Randall Feb 16 '22 at 11:59
58

You can run the following git command from the shell:

git log --pretty=oneline commit-id...HEAD
Stephen Warren
  • 240
  • 1
  • 9
Matthieu
  • 16,103
  • 10
  • 59
  • 86
18

Assuming that by "commit number", you mean commit hash:

git log <commit-hash>..HEAD
hammar
  • 138,522
  • 17
  • 304
  • 385
5

If anyone here is trying to find out how to LESS through the output of git log starting at a certain commit, paginating backward, it's as simple as git log <hash>.

Matthew Hinea
  • 1,872
  • 19
  • 31
1

As others have said, git log <commit-hash>..HEAD gets you started. git log <commit-hash>...HEAD is also suggested. I'm adding this answer because the number of periods can make a huge difference for diffs, so it may be worth understanding for git log also.

I don't understand it enough to explain it for git log behavior. For git diff branchA..branchB, the two-dot operator compares the files etc on branchB's tip to the state of files etc on branchA's tip. The three-dot operator compares the files etc on branchB's tip to the files etc in branchA at the commit where branchB diverged from branchA.

To me, this distinction can be important because three-dot might not show that similar changes were already made by some separate branch that got merged. i.e. the diff on a pull request might not show the current context of a patch to a function, if the function has changed since the branch diverged.

As an aside, GitHub, implicitly, uses a three-dot comparison on Pull Requests. GitHub Enterprise also uses three-dot. As of writing this, there is a way to see how to do a two dot comparison in github on the linked pages.

David Mann
  • 1,874
  • 2
  • 16
  • 19
1

Ranges like hash..HEAD don't seem to reliably yield the result in historical order, and options like --author-date-order seem to be ignored.

To get all commits historically since a given hash, I have found something like this to be the only correct solution (Bash):

git log --author-date-order --all --reverse --after="$(git show -s --format='%at' COMMIT_HASH)"
phil294
  • 10,038
  • 8
  • 65
  • 98
1

Just adding to the answer for the general case you can get all commits from one commit to another commit. For example lets sayA is commit hash start and B is commit hash end then

git rev-list <A>..<B>

for detailed

git log <A>..<B>
Faiz Hameed
  • 488
  • 7
  • 15