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.

  • Error 0x2 starting node.exe -v in Git bash for Windows
  • Exclude .svn directories from grep
  • Indenting Bash Script Output
  • How do I get Git's latest stable release version number?
  • Accidentally deleted many hidden files with “git clean -fd” and now I cannot install Ruby
  • Get SHA1 of latest remote commit
  • 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 reset a branch to another branch with git?
  • Is there a way to download only a subdirectory from git?
  • Concurrency in a GIT repo on a network shared folder
  • Why does `rm -rf` behave differently when used in a git post-receive hook as opposed to shell?
  • Git checkout and merge without touching working tree
  • Remove folder from git history: BFG tool deletes all branches
  • 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.