Validate if commit exists

后端 未结 6 1035
走了就别回头了
走了就别回头了 2020-12-10 02:45

How to validate whether the commit with given sha exists in current branch?

There are many ways to parse outputs, but I need optimal way which returns boolean (for u

6条回答
  •  粉色の甜心
    2020-12-10 03:22

    git merge-base --is-ancestor $sha HEAD
    

    This tests if $sha is an ancestor commit for the current branch (HEAD), and exits successfully if it is.

    In your example,

    sha=$1
    if ! git merge-base --is-ancestor $sha HEAD; then
      echo "Invalid commit sha: $sha"
      exit 1
    fi
    

提交回复
热议问题