Finding what branch a Git commit came from

后端 未结 14 2540
失恋的感觉
失恋的感觉 2020-11-22 10:43

Is there a way to find out what branch a commit comes from given its SHA-1 hash value?

Bonus points if you can tell me how to accomplish this using Ruby Grit.

14条回答
  •  自闭症患者
    2020-11-22 11:32

    git branch --contains is the most obvious "porcelain" command to do this. If you want to do something similar with only "plumbing" commands:

    COMMIT=$(git rev-parse ) # expands hash if needed
    for BRANCH in $(git for-each-ref --format "%(refname)" refs/heads); do
      if $(git rev-list $BRANCH | fgrep -q $COMMIT); then
        echo $BRANCH
      fi
    done
    

    (crosspost from this SO answer)

提交回复
热议问题