Validate if commit exists

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 usage in bash script).

  • git-subtree without squash: view log
  • NodeGit return blob oids at HEAD
  • Gitlab execute stage conditionally
  • Can I do 'git svn dcommit' from a SVN git clone which was created with --no-metadata?
  • Generating online books
  • git: Why doesn't git diff show any differences?
  • e.g.

    sha=$1
    if [ -z `git magic --validate $sha` ]; then
      echo "Invalid commit sha: $sha"
      exit 1
    fi
    

  • can I retrieve version of file from git for a given date?
  • How do I make a git branch's upstream reference be read-only?
  • git fails because of an untracked working tree file
  • Git-SVN with svn.pushmergeinfo: how to avoid self-referencing mergeinfo lines
  • running cooja in contiki with cmd “ant run”
  • How do you deal with configuration files in source control?
  • 6 Solutions collect form web for “Validate if commit exists”

    The rev-list | grep method works fine; there’s the tiniest bit of overhead because git has to print out all the SHA1s for grep to see, but it’s not really a big deal.

    You can also do it with git merge-base if you like – if the merge base of the target commit and the branch is the target commit, the branch contains the target commit:

    if [ "$(git merge-base $commit $branch)" = "$commit" ]; then
        ...
    fi
    

    Either way you do it, note that rev-list and merge-base are going to be printing out SHA1s, so if the commit you’re testing for inclusion is named by a branch or tag, you’ll want to use git rev-parse to turn it into an SHA1 first.

    git rev-parse --quiet --verify <commit>
    

    Does not actually verify that commit (I guess SHA1 is what is meant) exists. It verifies that there is an object in the database corresponding to the SHA1 provided. That is, if there is a blob or tree object that matches the SHA1, it will report that it exists, even if it is not a commit.

    git rev-parse --quiet --verify <sha1>^{commit}
    

    This will verify that the object exists and that it is an object that can be used as a commit (commit or annotated tag).

    You can look at the output of

    git rev-list HEAD..$sha
    

    If this command fails because the sha1 does not exist at all in the repository, the exit code will be non-zero. If the output is empty, the commit is in the current branch, and if it is non-empty, it is not. So your script would look like

    if revlist=`git rev-list HEAD..$sha 2>/dev/null` && [ -z "$revlist" ]; then
        :
    fi
    

    In case you already know that $sha really names a commit (as a SHA1 hash or in any other way), this simplifies to

    if [ -z "`git rev-list HEAD..$sha`" ]; then
        :
    fi
    
    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
    
    git rev-list branch-youre-interested-in | grep -q sha-youre-interested-in
    

    You can use the exit code from grep in conditionals.

    For the current branch,

     git rev-list HEAD | grep -q sha-youre-interested-in
    

    git cat-file prints information about the objects in the repository. “-e” checks if the object is valid and exits with a 0 status and when invalid exists with a 1.

    git cat-file -e 3d68db1028afe27a0055c2234f98fc945b1958f5
    echo $?
    1
    
    Git Baby is a git and github fan, let's start git clone.