Test to determine if git clone command succeeded

I tried to clone the git repository by passing the username, password. That was successful.

But what my intention is that I want to know whether the git clone command executed or not.
If not, I would like to handle such kind of errors in shell script itself.

  • How to enter command with password for git pull?
  • Git: Getting total numbers of uncomitted lines in a repo
  • Pyexcel_xlsx successfully installed but importing is still on syntax error
  • Why 'git ls-files' is not shown on tab completion?
  • Git commit hook - How to use the commit-msg hook to check for string in message?
  • Is it possible to remove a remote repository on Github through the command line?
  • My working shell script:

    cd ..
    git clone https://username:password@bitbucket.org/username/repositoryname.git
    cd repositoryname
    git checkout branchname1
    cd ..
    mv repositoryname newfoldername
    git clone https://username:password@bitbucket.org/username/respositoryname.git
    cd repositoryname
    git checkout branchname2
    cd ..
    mv repositoryname newfoldername
    

    How do I test, in the script, whether these steps were successful?

  • How to ignore files ended with ~ in git?
  • Git for Mac fails to launch: “illegal instruction”
  • Error when “git push” to github
  • How to do git merge/pull correctly: You have not concluded your merge (MERGE_HEAD exists)
  • Unlink of file failed
  • How do I add my whole project on GitHub?
  • 3 Solutions collect form web for “Test to determine if git clone command succeeded”

    The return value is stored in $?. 0 indicates success, others indicates error.

    some_command
    if [ $? -eq 0 ]; then
        echo OK
    else
        echo FAIL
    fi
    

    I haven’t tried it with git, but I hope this works.

    if some_command
    then
      echo "Successful"
    fi
    

    Example

    if ! git clone http://example.com/repo.git
    then
      echo "Failed"
    else
      echo "Successful"
    fi
    

    See How to detect if a git clone failed in a bash script.

    This one should work (Just put your script at the place marked “— your script here —” below):

    #!/bin/bash
    
    # call your script with set -e to stop on the first error
    bash <<EOF
    set -e
    --- your script here ---
    EOF
    
    # test status: I don't want this part to stop on the first error,
    # and that's why use the HERE document above to wrap a sub-shell for "set -e"
    if [ $? -eq 0 ]; then
      echo success
    else
      echo fail
    fi
    

    Alternatively, HERE document can be replaced by:

    (
      set -e
      --- your script here ---
    )
    
    Git Baby is a git and github fan, let's start git clone.