Git worktree allows you to work on different different branches checked out in different folders under the same Git repo. This means they share the same Git history.
To demo that, let’s create worktree-demo folder and initialize the repo with main branch and one file.
mkdir worktree-demo
cd worktree-demo
git init
git branch -m main
git config user.name "Demo"
git config user.email "demo@example.com"
echo "Hello from main" > app.txt
git add app.txt
git commit -m "Initial commit"
Then let’s create another worktree for the feature-update branch
git worktree add ../worktree-feature -b feature-update
Folder structure now looks like (I cd’d up one level first):
tree .
.
├── worktree-demo
│ └── app.txt
└── worktree-feature
└── app.txt
Worktree list shows this:
git worktree list
worktree-demo ff49b47 [main]
worktree-feature ff49b47 [feature-update]
Let’s try to make a commit in the new worktree folder
cd ../worktree-feature
echo "Hello from feature branch" > app.txt
git status
git add app.txt
git commit -m "Update app text"
To prove that they share the same Git history, perform git log in any of the worktree folders. The log shows that both commits are in the same repo history.
git log --oneline --graph --all
* a65991e (feature-update) Update app text
* ff49b47 (HEAD -> main) Initial commit