日本語

How to Open Another Branch in a Separate Folder with git worktree

​​Git​command line

Sometimes, in the middle of your work, you need to open another branch for an urgent bug fix or a review. With git worktree, you can open another branch in a separate folder at the same time and leave your current folder's work-in-progress changes as they are. You do not need to run git stash or make a commit just to switch branches.

Note

I tested on Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates with environment variables. I created the working folder as /path/to, so the paths in the output use that value. I only used repositories I created locally. I did not test operations that use remote branches, git worktree move, lock, or repair, repositories with submodules, or Windows and macOS.

The state before the operations

I created a repository in an app folder and set it up as follows. I made the first commit on the main branch, then created a hotfix branch that points to the same commit. .env is listed in .gitignore, so Git does not track it. Finally, I added one line to index.html so that it had uncommitted changes.

console
$ echo '<h1>ねこノート</h1>' > index.html

$ printf 'node_modules/\n.env\n' > .gitignore

$ git add .

$ git commit -q -m '最初のコミット'

$ git branch hotfix

$ echo 'API_KEY=dummy' > .env

$ echo '<p>書きかけの記事一覧</p>' >> index.html

This is the state at this point. There is only one folder, app.

console
$ git status --short
 M index.html

$ git branch -v
  hotfix 43160d7 最初のコミット
* main   43160d7 最初のコミット

$ git worktree list
/path/to/app  43160d7 [main]

Opening an existing branch in a separate folder

git worktree add <folder> <branch> creates the specified folder and checks out the branch there. From inside app, I opened the hotfix branch in the neighboring app-hotfix folder.

console
$ git worktree add ../app-hotfix hotfix
Preparing worktree (checking out 'hotfix')
HEAD is now at 43160d7 最初のコミット

$ git worktree list
/path/to/app         43160d7 [main]
/path/to/app-hotfix  43160d7 [hotfix]

git worktree list now shows the original app (main) and the new app-hotfix (hotfix). The official git worktree reference calls the original folder the "main worktree" and a folder added with add a "linked worktree."

In the app-hotfix folder, .git was a file, not a folder. Its contents point to the administrative folder for this worktree, which is inside the original repository. The untracked .env file was not present in the new folder.

console
$ ls -a ../app-hotfix
.
..
.git
.gitignore
index.html

$ cat ../app-hotfix/.git
gitdir: /path/to/app/.git/worktrees/app-hotfix

$ git -C ../app-hotfix status
On branch hotfix
nothing to commit, working tree clean

$ git status --short
 M index.html

The new folder has no work-in-progress changes, and the original app still has its index.html changes. If you need a file that Git does not manage, such as .env, in the new folder too, you have to copy it yourself.

Committing in each folder

I moved to app-hotfix, fixed the heading, and committed.

console
$ cd ../app-hotfix

$ sed -i 's/ねこノート/ねこノート(修正版)/' index.html

$ git commit -q -am '見出しを直す'

$ git log --oneline -1
e92026d 見出しを直す

I went back to app and ran git log hotfix, which showed the commit I made in app-hotfix. The work-in-progress changes in app were unchanged.

console
$ cd ../app

$ git log --oneline hotfix
e92026d 見出しを直す
43160d7 最初のコミット

$ git status --short
 M index.html

$ cat index.html
<h1>ねこノート</h1>
<p>書きかけの記事一覧</p>

According to the official reference, a linked folder is connected to the original repository, and everything is shared except per-worktree items such as HEAD and the staging area (index). Branches and tags, which live under refs/, are shared, and the repository configuration file (config) is normally shared across all folders. In the example above, the commit I made in app-hotfix was visible from app without any fetch or push.

One repository in app/.git is shared by the app/ folder on branch main and the app-hotfix/ folder on branch hotfix. HEAD, the staging area, and working files are separate for each folder. The same branch cannot be open in two folders at once
How one repository is used through two folders

Creating a new branch and opening it

To start work on a branch that does not exist yet, add -b <branch name>. If you omit the starting commit, the branch is created from the current HEAD. When you give only a folder, a branch with the same name as the folder is created.

console
$ git worktree add -b feature ../app-feature
Preparing worktree (new branch 'feature')
HEAD is now at 43160d7 最初のコミット

$ git worktree add ../app-try
Preparing worktree (new branch 'app-try')
HEAD is now at 43160d7 最初のコミット

$ git branch
+ app-try
+ feature
+ hotfix
* main

$ git worktree list
/path/to/app          43160d7 [main]
/path/to/app-feature  43160d7 [feature]
/path/to/app-hotfix   e92026d [hotfix]
/path/to/app-try      43160d7 [app-try]

In git branch output, * marks the branch of the current folder, and + marks a branch that is open in another folder (official git branch reference).

The same branch cannot be open in two folders

When I tried to open hotfix, which was already open in app-hotfix, in another folder, Git stopped me. Running git switch to hotfix from another folder failed the same way. While the branch is open, you also cannot delete it with git branch -d.

console
$ git worktree add ../app-hotfix2 hotfix
Preparing worktree (checking out 'hotfix')
fatal: 'hotfix' is already used by worktree at '/path/to/app-hotfix'

$ git -C ../app-feature switch hotfix
fatal: 'hotfix' is already used by worktree at '/path/to/app-hotfix'

$ git branch -d hotfix
error: cannot delete branch 'hotfix' used by worktree at '/path/to/app-hotfix'

According to the official reference, adding --force to git worktree add lets you open a branch that is already open in another folder.

Cleaning up folders you no longer need

Use git worktree remove to delete a folder you no longer need. I put an untracked memo.txt in app-feature and tried to remove it. Git refused.

console
$ echo 'メモ' > ../app-feature/memo.txt

$ git worktree remove ../app-feature
fatal: '../app-feature' contains modified or untracked files, use --force to delete it

$ git worktree remove --force ../app-feature

$ git worktree remove app-try

$ git worktree list
/path/to/app         43160d7 [main]
/path/to/app-hotfix  e92026d [hotfix]

$ git branch
  app-try
  feature
+ hotfix
* main

I could remove app-try without --force because it had no changes. As the official reference says, if the last part of the path does not overlap with others, you can specify just the folder name, as I did with app-try. Removing the folders left the feature and app-try branches in place. If you also no longer need a branch, delete it separately with git branch -d. I did this at the end of the next section.

Warning

git worktree remove --force deletes the folder along with any uncommitted changes and untracked files. In the example above, memo.txt was deleted too. Before using it, run git status in that folder and check that nothing you want to keep is there.

If you deleted a folder directly

If you delete a folder with rm -rf or a similar command instead of git worktree remove, the repository keeps the administrative information for that worktree. git worktree list shows it as prunable. You can clean up this information with git worktree prune.

console
$ rm -rf ../app-hotfix

$ git worktree list
/path/to/app         43160d7 [main]
/path/to/app-hotfix  e92026d [hotfix] prunable

$ git worktree prune -v
Removing worktrees/app-hotfix: gitdir file points to non-existent location

$ git worktree list
/path/to/app  43160d7 [main]

$ git branch
  app-try
  feature
  hotfix
* main

$ git branch -d feature app-try
Deleted branch feature (was 43160d7).
Deleted branch app-try (was 43160d7).

$ git branch
  hotfix
* main

After prune, the + before hotfix disappeared, so Git no longer treats it as open in another folder. I also deleted the leftover feature and app-try branches with git branch -d at this point. According to the official reference, administrative information for a folder deleted without git worktree remove is eventually removed automatically even if you leave it alone. The time before that happens is set by the gc.worktreePruneExpire configuration.

​Posted by 86