Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

My working shell script:

cd ..
git clone https://username:[email protected]/username/repositoryname.git
cd repositoryname
git checkout branchname1
cd ..
mv repositoryname newfoldername
git clone https://username:[email protected]/username/respositoryname.git
cd repositoryname
git checkout branchname2
cd ..
mv repositoryname newfoldername

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

like image 787
bhadram Avatar asked Nov 06 '14 04:11

bhadram


People also ask

How do I know if my branch is cloned?

You successfully cloned a Git repository into a specific folder on your server. In this case, you cloned the master branch from your Git remote repository. You can check the current branch cloned by running the “git branch” command.

Does git clone get all history?

Cloning an entire repo is standard operating procedure using Git. Each clone usually includes everything in a repository. That means when you clone, you get not only the files, but every revision of every file ever committed, plus the history of each commit.

How do I verify a git repository?

Use the git status command, to check the current state of the repository.


2 Answers

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.

like image 179
Ajay Avatar answered Oct 23 '22 19:10

Ajay


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.

like image 30
timofey.com Avatar answered Oct 23 '22 18:10

timofey.com