Submodule vs Worktree#

Submodule#

Manages external repositories as subdirectories. It points to a specific commit and is suitable for dependency management such as libraries or themes. eg. This blog manages the Hugo theme (hugo-book) as a submodule.

How to update the parent repository#

  1. In the Standalone Submodule Repo:
    • Perform work, commit, and push.
    • Note: You never need git sync here because there are no submodules. See gitconfig for the alias definition.
  2. In the Parent Repo:
    • Run git submodule update --remote.
    • The parent repo will notice the submodule has moved to a new commit.
    • Commit and push the new pointer in the parent repo.

Worktree#

Creates multiple working directories from a single repository. Useful for working on multiple branches simultaneously, reducing context switching costs.

Usage Example#

Used when you need to fix an urgent bug without stopping current work or using stash.

# Create and checkout a hotfix branch in a new directory
git worktree add ../hotfix-dir main

# Move to the directory and perform work
cd ../hotfix-dir
# Fix and commit
git commit -am "fix urgent bug"

# Return to the original working directory
cd -

# Remove the worktree after completion
git worktree remove ../hotfix-dir

# git