🪴 notes

Search

Search IconIcon to open search

using git worktrees

Published Oct 31, 2024 Last updated Oct 31, 2024 Edit Source

worktrees allow you to check out multiple branches at the same time. it is nice when you want to refer to content not available on the current branch you are working on

the repo structure should look:

1
2
3
4
project/
	project.git
	main(worktree)
	feat-1(worktree)

each worktree is a different branch and can be viewed independently! git commands can only be ran within the worktrees or in the .git directory. now you don’t change branches with checkout, just cd into the worktree

# creating from scratch

source

1
2
3
4
5
6
7
mkdir project
cd project
git init --bare project.git
cd project.git

git worktree add ../main (creates a worktree from branch main or creates branch if doesn't exist)
cd ../main

# cloning

1
2
3
4
5
6
7
8
9
mkdir project
cd project
git clone remoterepo --bare 


cd project.git

git worktree add ../main 
cd ../main

# turning “normal” repo into bare

source

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
mkdir worktree_project
cd worktree_project

git clone --mirror ../project_repo_path ./project.git

// rename worktree_project to project
cd ..
rm -rf project
mv ./worktree_project ./project

cd project
git worktree add ../main 
cd ../main