1

I am storing the revision numbers of certain git commits, in which those revision number's are fetched with the following command.

git rev-list --count <commit-hash> 

I'm wondering if there is a command that will return the commit-hash given the revision number, i.e. let's say the command returns 62383, what command can I use to get the commit hash back from this revision number?

2
  • 4
    Suppose your git repo has exactly 3 commits. A and B each have the parent C, which is the root commit. In that case, the revision number for each of A and B is 2. Which commit would you like to use? Commented Jun 27, 2022 at 20:04
  • 2
    This is not a revision number. This is a sum. It counts how many commits are reachable from <commit-hash>. Not commit-hash's supposed revision number. Commented Jun 28, 2022 at 7:29

1 Answer 1

3

Note: rev-list lists all the commits that are reachable by the commit you provide. The --count option just tells you how many are in that list, so it doesn't really make sense to call that number a "revision". It also may not make much sense to assign meaning to the Nth commit going forward in time, especially if the history is not linear. For this reason I suspect your question may be an X-Y Problem.

That being said, your question is answerable as asked, and you can probably achieve what you want by picking out the Nth commit ID from the list. Here's an example in *nix shells:

git rev-list @ --reverse | head -N | tail -1 # set N to the number you want 

Note you still must provide a commit to the rev-list command, and depending on which branch or commit you use, the results may be different. In the example I use @ which is shorthand for HEAD which is the tip commit of your currently checked out branch. You could probably use whatever commit you used when running your command in the first place (for any higher N), or perhaps a particular branch name that is ahead of the commit you're wishing to find.

Side Note: I believe if you get to the root of the problem, you'll most likely wish to solve it a different way.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.