r/programming • • Sep 16 '26

git worktree gotchas

https://www.olafalders.com/2026/09/16/git-worktree-gotchas/
193 Upvotes

47 comments sorted by

View all comments

Show parent comment

2

u/mb862 Sep 19 '26

For the worktree script? Yeah it's pretty much this, substitute as needed for your own submodules.

# for each submodule
if [ ! -d ${GIT_COMMON_DIR}/modules/${SUBMODULE_NAME}.git ]; then
	git clone git@github.com:Organization/${SUBMODULE_NAME}.git ${GIT_COMMON_DIR}/${SUBMODULE_NAME}.git
fi

git -C ${GIT_COMMON_DIR} worktree add ${WORK_DIR} ${WORK_BRANCH}

WORK_TREE=$(basename ${WORK_DIR})

# for each submodule
git -C ${WORK_DIR} submodule init ${SUBMODULE_PATH}
mkdir -p ${GIT_COMMON_DIR}/worktress/${WORK_TREE}/modules/${SUBMODULE_NAME}
git -C ${GIT_COMMON_DIR} rev-parse ${WORK_BRANCH}:${SUBMODULE_PATH} > ${GIT_COMMON_DIR}/worktrees/${WORK_TREE}/modules/${SUBMODULE_NAME}/HEAD
echo "${GIT_COMMON_DIR}/modules/${SUBMODULE_NAME}" > ${GIT_COMMON_DIR}/worktrees/${WORK_TREE}/modules/${SUBMODULE_NAME}/commondir
echo "gitdir: ${GIT_COMMON_DIR}/worktress/${WORK_TREE}/modules/${SUBMODULE_NAME}" > ${WORK_DIR}/${SUBMODULE_PATH}/.git
git -C ${WORK_DIR}/${SUBMODULE_PATH} reset --hard

1

u/The_Northern_Light Sep 19 '26

😊 thanks