0

I know that I can use git rev-list --count master to get the number of commits on the specified branch - I use this to add a build number to the apps I release.

Is there a nice way in git to use that number to checkout a specific commit i.e. something like git checkout 45 master, which would checkout the 45th commit on the master branch?

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Git works *backwards* (because it has to, and also because that's the most natural: we want the latest! ok, that didn't work, how about the next-to-latest?) so you'll find it works better for you if you start thinking backwards too. :-) – torek Oct 08 '21 at 20:38
  • @torek "we want the latest" - except in this case, where I want the exact commit that this version of my app was built using, which probably _isn't_ the latest. I do have the commit ref in the app's settings screen, but that's no use in a crash report from Apple which just gives a build number (which has to be numeric, bah) - so that's all I have to go on. – deanWombourne Oct 11 '21 at 07:54
  • I know, I'm speaking in general terms here (people want "the latest"). The Apple limitation is its own thing; you might be well-served here by having a database where you map a unique number to a Git hash ID, and add to the DB whenever you make a release. – torek Oct 11 '21 at 08:07

1 Answers1

0

You can check out the 45th commit back from the current master with git checkout master~45. You will need to do some maths if you want to count from the root forward instead of from master backwards.

What's the difference between HEAD^ and HEAD~ in Git? has more details.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Unfortunately, it's the counting forwards bit I need, sadly. :( – deanWombourne Oct 11 '21 at 07:51
  • @deanWombourne You should be able to do this with some math. Based on the total number of commits and which commit you want counting forwards, you can easily calculate how many you would have to count backwards to get to the same commit. – Code-Apprentice Oct 11 '21 at 20:33