Michael Fourie
Michael Fourie

Reputation: 11

Get git commit hash from the revision number

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?

Upvotes: 1

Views: 713

Answers (1)

TTT
TTT

Reputation: 28944

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.

Upvotes: 3

Related Questions