- 以下を適当な名前で保存する。
function usage_git_subdir_clone() { cat << EOF usage: git_subdir_clone [URL] [subdir] EOF exit 0 } function git_subdir_clone() { URL=$1 DIRS=$2 if [ ""${URL} = "" ] ; then usage_git_subdir_clone fi if [ ""${DIRS} = "" ] ; then usage_git_subdir_clone fi GITNAME=$(basename ${URL} .git) TARGET_DIR=${GITNAME}/${DIRS} if [ -d ${TARGET_DIR} ] ; then echo "exist ${TARGET_DIR}" exit 0 fi CWD=$(readlink -f .) mkdir -p ${GITNAME} cd ${GITNAME} git init git config core.sparsecheckout true git remote add origin ${URL} git sparse-checkout set ${DIRS} git fetch --depth=1 BRANCH=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}') git checkout origin/${BRANCH} git branch -D ${BRANCH} 2>/dev/null git checkout -b ${BRANCH} git branch -u origin/${BRANCH} cd ${CWD} }
- .bashrcから読みこむ
例えば~/.bin/git_subdir_clone
に保存した場合
~/.bashrc...<略> . ${HOME}/.bin/git_subdir_clone
- あとはコマンドとして呼ぶだけ
例)git_subdir_clone https://github.com/unicode-org/icu.git icu4c/source
参考
https://qiita.com/progrhyme/items/994f738be668bf3dc452
https://itosae.com/archives/7642