LoginSignup
0
0

More than 5 years have passed since last update.

特定のブランチをgit clone || そのブランチがなければgit checkout

Posted at

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などから日付のブランチにコミットしたいみたいな用途で必要になった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0