Shell: don’t fail git clone if folder already exists

The most stable solution would be to simply let it fail and print the error message. If you think that’s too ugly for your scenario, you may redirect it to /dev/null:

folder="foo"
if ! git clone "${url}" "${folder}" 2>/dev/null && [ -d "${folder}" ] ; then
    echo "Clone failed because the folder ${folder} exists"
fi

Otherwise you can do something like this:

if [ ! -d "$FOLDER" ] ; then
    git clone "$URL" "$FOLDER"
fi

but that would be vulnerable to race conditions.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)