This repository contains a helper script setup-git-worktree.sh that automates the creation of a bare Git repository with adjacent worktree directories, following the flat worktree architecture described in Tom Ups' article:
The layout created by the script looks like this:
project/
├── .bare/ # Bare repository (the actual Git database)
├── .git # Text file with "gitdir: ./.bare"
├── main/ # Primary worktree (checked out `main` branch)
└── gitworktree.sh # Helper to add additional worktrees
Key characteristics:
- Zero nesting – each worktree is a sibling of the others; no worktree lives inside another.
- Shared object database – all worktrees share the same
.barerepository, keeping disk usage low. - Convenient helper –
gitworktree.shcreates new worktrees adjacent tomain, preserving any slashes in the branch name (e.g.,feat/awesomecreatesfeat/awesome/).
chmod +x setup-git-worktree.sh
./setup-git-worktree.sh <git-url>Replace <git-url> with either an HTTPS URL (https://github.com/org/repo) or an SSH URL (git@github.com:org/repo.git).
The script will:
- Parse the organization and repository name.
- Create the directory
$ORG/$REPO. - Clone the repository as a bare repo into
.bare. - Write a
.gitfile that points to.bare. - Add a
mainworktree. - Generate
gitworktree.shfor future worktree creation.
cd $ORG/$REPO
./gitworktree.sh <branch-name><branch-name>can contain slashes, e.g.,feat/new-feature→ createsfeat/new-feature/.- The script validates the branch name and runs
git worktree add.
git worktree listThe script implements the "Worktrees level 2: bare repo" approach from Tom Ups' article, which explains how to keep a clean, flat directory structure while using multiple worktrees.
Feel free to open issues or submit pull requests if you have improvements!