1

I have an orphan branch (let's call it output) which contains the documents generated by templates stored on my main branch. I would like to checkout the commit on output that correspond to a specific commit on main.

I settled on using git commit --trailer 'Source: xxxxx' when committing on output where xxxxx is the corresponding commit on main.

Is it possible to checkout a commit on output knowing only the value of its trailer?

phd
  • 82,685
  • 13
  • 120
  • 165
ITChap
  • 4,057
  • 1
  • 21
  • 46
  • 2
    "orphan branch" is not a very well defined term in Git; I presume here (and so did phd, in his answer, I think) that you mean "a branch with no shared history". (ElpieKay didn't make this same leap.) – torek Dec 02 '22 at 20:10

3 Answers3

2

To find the sha1 value of a commit that has the trailer Source: xxxxx,

git log --pretty=%H --grep='Source: xxxxx'

To check out the commit in one step,

git checkout $(git log --pretty=%H --grep='Source: xxxxx')
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
2

To be sure to match a commit with a trailer (and not just a commit message whose topic happens to include Source: xxxxx), you can use the "%(trailers[:options])" format.
And use git switch (not checkout), or git show to just see the content (without modifying the current working tree)

git switch \
$(git log --pretty=format:"%H% (trailers:key=Source,valueonly)"|grep Junio|head -1|cut -d " " -f1)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This seems to offer more guarantees than @phd's answer. Just to confirm what would happen if by any chance git log was to return more than one commit? – ITChap Dec 05 '22 at 12:41
  • 1
    @ITChap The `head -1` part would pick the first one (which would be the most recent one) – VonC Dec 05 '22 at 12:44
1
git show ':/Source: xxxxx'

See the docs on :/ in git help revisions.

phd
  • 82,685
  • 13
  • 120
  • 165