3

I get a position of commit by command

git rev-list HEAD --count

For example, output is 1164. I want checkout to this commit number. How can I do this? I think, need get hash of this commit and checkout to commit by it hash.

whalemare
  • 1,107
  • 1
  • 13
  • 30
  • 1
    Git is not made to work with commit number which is also wrong because you could have multiple commit with this count number. Use the hash which is the only unique id. – Philippe Jul 05 '18 at 12:24
  • Hmm... what commit do you denote by *"commit #1164"*? The first one displayed by `git rev-list`? Or the last one? – axiac Jul 05 '18 at 12:30

3 Answers3

7

Here's another solution:

git rev-list HEAD --reverse | nl | grep <your build number>

git rev-list HEAD --reverse prints out all the commit hashes from oldest to newest. nl prefixes each line with a line number (effectively the count). And finally, good ol' grep filters to just the line you want.

You could further refine this to just the commit hash with awk:

git rev-list HEAD --reverse | nl | grep <your build number> | awk '{print $2}'

or take it to the next level with a git alias:

git config --global alias.build-number '!f() { git rev-list HEAD --reverse | nl | grep $1 | awk '"'"'{print $2}'"'"'; }; f'

Alias usage:

git build-number <your build number>

That quoting madness in the git config command above is explained here: https://stackoverflow.com/a/1250279/320737

James H
  • 1,824
  • 22
  • 23
3

By adding --count to the git rev-list command line you tell it to not display the revisions list but only the number of revisions it would display without the --count argument.

Basically git rev-list HEAD --count and git rev-list HEAD | wc -l produce the same output (the number of lines in the output of git rev-list HEAD).


The answer to your question depends on what exactly do you mean by "commit #1164".

If you count the commits as they are listed by rev-list then the first commit is the current commit (aka HEAD) and commit #1164 is the oldest commit that is reachable starting from HEAD. To get its hash, don't tell git rev-list to display the count but let it display the revisions (it displays their hashes) and pipe its output to tail -1 to get the last hash in the list:

git rev-list HEAD | tail -1

But please note that by adding a new commit to the current branch, the numbering will change and commit #1164 will be a different commit.


If you count the commits in chronological order, commit #1 being the first commit created in the repository then commit #1164 is the current commit. In this case you don't even need to use git rev-list (it walks the entire repository but you don't need this). It's enough to use:

git rev-parse HEAD

to get the hash of the current commit (HEAD).


Either way, the number you produced (1164) does not have any relevance to Git and it is not associated in any way with any commits. If you check out a different branch that has at least 1164 commits and follow the same procedure you get a different commit as commit #1164.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • `git rev-parse` is better for that, no!?! – Philippe Jul 05 '18 at 12:26
  • I think we understood the question in different manners. And it's highly probable that I'm not the one that understood it correctly :-( – axiac Jul 05 '18 at 12:32
  • Sure, if the OP counts the commits in chronological order, *"commit #1164"* on the current branch would be the current commit and there is no need for `git rev-list`, a simple call to `git rev-parse HEAD` is enough, indeed. – axiac Jul 05 '18 at 12:33
0

Thanks all for yours answers, but it not what I was looking for. I know that use git --count it`s not good idea, but at the moment we mark our bilds by this method.

I write little bash snippet, that doing what I need.

#!/bin/bash
SEARCH=$1
LAST=$(git rev-list HEAD --count)
DIFF=$((LAST-SEARCH))
git rev-list --skip=$DIFF --topo-order HEAD | head -n1

Where SEARCH it is --count of commit that you what to search.

whalemare
  • 1,107
  • 1
  • 13
  • 30