Skip to content

Instantly share code, notes, and snippets.

@peterneave
Created July 16, 2024 07:00
Show Gist options
  • Save peterneave/3f9514f66de59599f9a8cdfc74a4a443 to your computer and use it in GitHub Desktop.
Save peterneave/3f9514f66de59599f9a8cdfc74a4a443 to your computer and use it in GitHub Desktop.
Setup Git Worktree
#!/usr/bin/env bash
set -e
# Examples of call:
# git-clone-bare-for-worktrees git@github.com:name/repo.git
# => Clones to a /repo directory
#
# git-clone-bare-for-worktrees git@github.com:name/repo.git my-repo
# => Clones to a /my-repo directory
url=$1
basename=${url##*/}
name=${2:-${basename%.*}}
mkdir $name
cd "$name"
# Moves all the administrative git files (a.k.a $GIT_DIR) under .bare directory.
#
# Plan is to create worktrees as siblings of this directory.
# Example targeted structure:
# .bare
# main
# new-awesome-feature
# hotfix-bug-12
# ...
git clone --bare "$url" .bare
echo "gitdir: ./.bare" > .git
# Explicitly sets the remote origin fetch so we can fetch remote branches
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
# Gets all branches from origin
git fetch origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment