git clone時に--branch ブランチ名 --single-branch
を使うと、特定のブランチだけをcloneできるので高速。
しかし、特定のブランチがそのリポジトリにない場合は、git cloneがコケてしまう。
そのため、git clone
がコケたら、普通にcloneしてgit checkout
するというものを書いた。
branchName="test-branch"
tmpDir="/tmp/project"
git clone --depth 1 --branch "${branchName}" --single-branch https://github.com/jser/jser.github.io.git "${tmpDir}"
if [ $? -ne 0 ]; then
echo "Not found ${branchName} branch and git clone"
git clone --depth 1 https://github.com/jser/jser.github.io.git "${tmpDir}"
fi
cd "${tmpDir}"
git checkout "${branchName}"
Travis CIなどから日付のブランチにコミットしたいみたいな用途で必要になった。