git worktree Notes

Russell Bateman
April 2023
last update:

Getting started...

git worktree requires that you create a bare clone of your repository.

Second experiment...

~ $ go git-playground
~/dev/git-playground ~
~/dev/git-playground $ git init tutorial
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: 	git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: 	git branch -m <name>
Initialized empty Git repository in /home/russ/dev/git-playground/tutorial/.git/
~/dev/git-playground $ cd tutorial/
~/dev/git-playground/tutorial $ git worktree list
/home/russ/dev/git-playground/tutorial  0000000 [master]
~/dev/git-playground/tutorial $ echo hello world > temp.txt
~/dev/git-playground/tutorial $ git add .
~/dev/git-playground/tutorial $ gs
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   temp.txt

~/dev/git-playground/tutorial $ git commit -m "initial commit"
[master (root-commit) d7e4050] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 temp.txt
~/dev/git-playground/tutorial $ git log
commit d7e405004a4d67e0f134759feee8defba4603a9e (HEAD -> master)
Author: Russell Bateman <[email protected]>
Date:   Thu Apr 27 16:29:58 2023 -0600

    initial commit
~/dev/git-playground/tutorial $ git branch
* master
~/dev/git-playground/tutorial $ git worktree add -b tutorial-ccda-2019 ../tutorial-ccda-2019 master
Preparing worktree (new branch 'tutorial-ccda-2019')
HEAD is now at 9fee7d9 initial commit
~/dev/git-playground/tutorial $ git worktree add -b tutorial-ccda-2022 ../tutorial-ccda-2022 master
Preparing worktree (new branch 'tutorial-ccda-2022')
HEAD is now at 9fee7d9 initial commit
~/dev/git-playground/tutorial $ git branch
* master
+ tutorial-ccda-2019
+ tutorial-ccda-2022

Here's some semi-real playing around:

~/dev/git-playground/tutorial $ vim basic.cpp
~/dev/git-playground/tutorial $ ll
total 20
drwxrwxr-x 3 russ russ 4096 Apr 28 14:38 .
drwxrwxr-x 5 russ russ 4096 Apr 28 11:53 ..
-rw-rw-r-- 1 russ russ   20 Apr 28 14:38 basic.cpp
drwxrwxr-x 9 russ russ 4096 Apr 28 11:52 .git
-rw-rw-r-- 1 russ russ   12 Apr 28 11:50 temp.txt
~/dev/git-playground/tutorial $ git add .
~/dev/git-playground/tutorial $ git commit -m "another initial commit"
[master 229e316] another initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 basic.cpp
~/dev/git-playground/tutorial $ cd ../tutorial-ccda-2022/
~/dev/git-playground/tutorial-ccda-2022 $ gvim ccd-2022.cpp
~/dev/git-playground/tutorial-ccda-2022 $ ll
total 20
drwxrwxr-x 2 russ russ 4096 Apr 28 14:39 .
drwxrwxr-x 5 russ russ 4096 Apr 28 11:53 ..
-rw-rw-r-- 1 russ russ   27 Apr 28 14:39 ccd-2022.cpp
-rw-rw-r-- 1 russ russ   81 Apr 28 11:53 .git
-rw-rw-r-- 1 russ russ   12 Apr 28 11:53 temp.txt
~/dev/git-playground/tutorial-ccda-2022 $ git add .
~/dev/git-playground/tutorial-ccda-2022 $ gvim commit -m "another initial commit, this time for CCD 2022."
2 files to edit
~/dev/git-playground/tutorial-ccda-2022 $ git commit -m "another initial commit, this time for CCD 2022."
[tutorial-ccda-2022 197c87b] another initial commit, this time for CCD 2022.
 1 file changed, 1 insertion(+)
 create mode 100644 ccd-2022.cpp
~/dev/git-playground/tutorial-ccda-2022 $ git merge master
Merge made by the 'ort' strategy.
 basic.cpp | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 basic.cpp
~/dev/git-playground/tutorial-ccda-2022 $ cd ../tutorial-ccda-2019
~/dev/git-playground/tutorial-ccda-2019 $ git merge master
Updating 9fee7d9..229e316
Fast-forward
 basic.cpp | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 basic.cpp
~/dev/git-playground/tutorial-ccda-2019 $ cd ..
~/dev/git-playground $ tree
.
├── tutorial
│   ├── basic.cpp
│   └── temp.txt
├── tutorial-ccda-2019
│   ├── basic.cpp
│   └── temp.txt
└── tutorial-ccda-2022
    ├── basic.cpp
    ├── ccd-2022.cpp
    └── temp.txt

3 directories, 7 files

First experiment...

~ $ go git-playground
~/dev/git-playground ~
~/dev/git-playground $ git init tutorial
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: 	git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: 	git branch -m <name>
Initialized empty Git repository in /home/russ/dev/git-playground/tutorial/.git/
~/dev/git-playground $ cd tutorial/
~/dev/git-playground/tutorial $ git worktree list
/home/russ/dev/git-playground/tutorial  0000000 [master]
~/dev/git-playground/tutorial $ echo hello world > temp.txt
~/dev/git-playground/tutorial $ git add .
~/dev/git-playground/tutorial $ gs
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   temp.txt

~/dev/git-playground/tutorial $ git commit -m "initial commit"
[master (root-commit) d7e4050] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 temp.txt
~/dev/git-playground/tutorial $ git log
commit d7e405004a4d67e0f134759feee8defba4603a9e (HEAD -> master)
Author: Russell Bateman <[email protected]>
Date:   Thu Apr 27 16:29:58 2023 -0600

    initial commit
~/dev/git-playground/tutorial $ git checkout -b new-feature
Switched to a new branch 'new-feature'
~/dev/git-playground/tutorial $ git branch
  master
* new-feature
~/dev/git-playground/tutorial $ echo a new line >> temp.txt
~/dev/git-playground/tutorial $ # What if we needed to stop working on new-feature to perform a hot fix?
~/dev/git-playground/tutorial $ git worktree add -b hotfix ../tutorial-hotfix master
Preparing worktree (new branch 'hotfix')
HEAD is now at d7e4050 initial commit
~/dev/git-playground/tutorial $ git worktree list
/home/russ/dev/git-playground/tutorial         d7e4050 [new-feature]
/home/russ/dev/git-playground/tutorial-hotfix  d7e4050 [hotfix]
~/dev/git-playground/tutorial $ ll ..
total 16
drwxrwxr-x  4 russ russ 4096 Apr 27 16:33 .
drwxrwxr-x 70 russ russ 4096 Apr 27 16:17 ..
drwxrwxr-x  3 russ russ 4096 Apr 27 16:29 tutorial
drwxrwxr-x  2 russ russ 4096 Apr 27 16:33 tutorial-hotfix
~/dev/git-playground/tutorial $ git checkout new-feature
M	temp.txt
Already on 'new-feature'
~/dev/git-playground/tutorial $ pushd ../tutorial-hotfix/
~/dev/git-playground/tutorial-hotfix ~/dev/git-playground/tutorial ~
~/dev/git-playground/tutorial-hotfix $ git branch
* hotfix
  master
+ new-feature
~/dev/git-playground/tutorial-hotfix $ git checkout new-feature
fatal: 'new-feature' is already checked out at '/home/russ/dev/git-playground/tutorial'
~/dev/git-playground/tutorial-hotfix $ git checkout -b hotfix
fatal: a branch named 'hotfix' already exists
~/dev/git-playground/tutorial-hotfix $ echo hotfix > fix.txt
~/dev/git-playground/tutorial-hotfix $ git add .
~/dev/git-playground/tutorial-hotfix $ git commit -m "hotfix"
[hotfix 8b94bff] hotfix
 1 file changed, 1 insertion(+)
 create mode 100644 fix.txt
~/dev/git-playground/tutorial-hotfix $ gs
On branch hotfix
nothing to commit, working tree clean
~/dev/git-playground/tutorial-hotfix $ git checkout master
Switched to branch 'master'
~/dev/git-playground/tutorial-hotfix $ git merge hotfix
Updating d7e4050..8b94bff
Fast-forward
 fix.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 fix.txt
~/dev/git-playground/tutorial-hotfix $ cd ../tutorial
~/dev/git-playground/tutorial $ gs
On branch new-feature
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   temp.txt

no changes added to commit (use "git add" and/or "git commit -a")
~/dev/git-playground/tutorial $ git worktree list
/home/russ/dev/git-playground/tutorial         d7e4050 [new-feature]
/home/russ/dev/git-playground/tutorial-hotfix  8b94bff [master]
~/dev/git-playground/tutorial $ git worktree remove ../tutorial-hotfix/
~/dev/git-playground/tutorial $ git worktree list
/home/russ/dev/git-playground/tutorial  d7e4050 [new-feature]
~/dev/git-playground/tutorial $ git add .
~/dev/git-playground/tutorial $ git commit -m "new feature"
[new-feature 9d7ebba] new feature
 1 file changed, 1 insertion(+)
~/dev/git-playground/tutorial $ git log
commit 9d7ebba68810926978939c11e69e20e32d16f9a2 (HEAD -> new-feature)
Author: Russell Bateman <[email protected]>
Date:   Thu Apr 27 16:42:58 2023 -0600

    new feature

commit d7e405004a4d67e0f134759feee8defba4603a9e
Author: Russell Bateman <[email protected]>
Date:   Thu Apr 27 16:29:58 2023 -0600

    initial commit
~/dev/git-playground/tutorial $ gs
On branch new-feature
nothing to commit, working tree clean
~/dev/git-playground/tutorial $ git branch
  hotfix
  master
* new-feature