How can I determine if a given git hash exists on a given branch?

前端 未结 3 1799
梦如初夏
梦如初夏 2021-01-30 20:54

Background: I use an automated build system which takes a git hash as input, as well as the name of the branch on which that hash exists, and builds it. However, the build syst

3条回答
  •  鱼传尺愫
    2021-01-30 21:30

    One possible non-solution would be to parse the result of:

    $ git reflog show myBranch
    

    and see if your hash is in it.

    C:\Prog\Git\tests\rep\main>git reflog show patches
    786a190 patches@{0}: rebase finished: refs/heads/patches onto 74630b983db476c323b1d3f6771e57484551240e
    8448d0f patches@{1}: master~1: updating HEAD
    74630b9 patches@{2}: commit: test2
    1e73e36 patches@{3}: branch: Created from master
    

    I keep this as a Community Wiki answer to remember Igor Zevaka and Chris Johnsen's comments:

    Would that work after a clone?
    Pretty sure your reflog starts after you clone a remote. So if a branch had a commit before the clone, it wouldn't show up in the reflog.

    There are also problems with pushes, rebases, fetches (for ‘remote tracking’ branches), and pulls (either merge or rebase type) where only the new tip commit ends up the reflog.
    Also reflogs are disabled by default in bare repositories (the most common destination for pushes).
    Also, if “cheating” is a problem, it is just a matter of editing a text file to add or delete reflog entries, but it is a bigger ordeal to alter the history graph itself.

提交回复
热议问题