UnchartedWaters UnchartedWaters - 1 year ago 44
Git Question

Why does git HEAD~[N] only refer to pull requests?

Everywhere I've looked, it is stated that to get the previous commit, do

git checkout HEAD~1


in a git repository. But when I do so, a number of commits are skipped. The repository in question is Google upb library.

Edit:

These are the last 10 commits in the referenced repository, obtained by running

git log --pretty=oneline

ae30b4a816a57a8101ce187245edc3050d515c06 Merge pull request #90 from haberman/gencode3
72c333eb037da6fe63358efe9c6dc29547b0ab49 Fixed amalgamated build.
058a17f6a6c2487fd674511d5409497a03e83758 Fix amalgamated build.
98f513fb8fa5e3360baeb59c37636ca71d562490 Fixed 32-bit build and added generated files.
6b8767422154008eed98d0df42e36758d38877a4 Address review comments and fix compile warnings.
806ffc1d2053f1c02167c7965b39abc997d12ad6 Responded to PR comments.
15308afff2d0d288b73c1b4278bd28f926ce02b8 Fixes for oneof conformance tests.
b697882fb272c2f3408f5c821a88aaf3e3db6f52 Fixed varint length when buffer is reallocated.
be9094d91a2da777002a0f713306ac1bb74a6ac5 New encode/decode: most (171 / 192) conformance tests pass.
d5167fff744167eca45df65b5b935525778e2886 Merge pull request #88 from google/gencode2


See for yourself. Running
git checkout HEAD~1
yields the following message

Previous HEAD position was ae30b4a... Merge pull request #90 from haberman/gencode3
HEAD is now at d5167ff... Merge pull request #88 from google/gencode2


So
HEAD
has moved back 9 commits, not 1.

Run
git log
again to get the commit history relative to
HEAD
(at
d5167ff
).

git log --pretty=oneline

d5167fff744167eca45df65b5b935525778e2886 Merge pull request #88 from google/gencode2
6eb489a30ab0bc4e859944650f13db7a706e27da Merge branch 'master' into gencode2
0af928564ef4bb1ad3aa7cef26929bab7836f876 Merge pull request #89 from google/stringview
1278ff899409025692d6915421f1af1c99dc837e Responded to PR comments.
c0a660f474384e0e3c7d4dd75c9b3e6b32daca9f Added upb_stringview, the string representation for upb_msg.


Running
git checkout HEAD~1
again yields

Previous HEAD position was d5167ff... Merge pull request #88 from google/gencode2
HEAD is now at 0af9285... Merge pull request #89 from google/stringview


Several commits have obviously been skipped.

Answer Source

git .... HEAD~n walks back n-grandparents. Referring the documentation

A suffix ~ to a revision parameter means the commit object that is the th generation grand-parent of the named commit object, following only the first parents. I.e. ~3 is equivalent to ^^^ which is equivalent to ^1^1^1. See below for an illustration of the usage of this form.

If you now have a merge commit, which you obviously have on your HEAD position walking back n-generation can lead to a jump in timeline to the next grandparent of that merge. In your case it's 0af9285

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download