A file manager shows only the files of the branch that is currently checked out. To look at another branch, we usually switch to it, and the files in the folder change under our feet.
git worktree offers an easier way: it opens a branch as a separate folder. Both folders stay available, and we can browse them like any other folders.
The problem with switching
Suppose we have a project with two branches, main and draft, and draft holds an extra file, notes.md. On main, we cannot see it:
lsreport.mdWe could run git switch draft, look at the file, and switch back. That gets tiring, and unfinished changes can follow us from one branch to the other or block the switch.
Give the branch its own folder
From inside the project folder, we run:
git worktree add ../project-draft draftPreparing worktree (checking out 'draft')
HEAD is now at 4f9ea19 Add notesThe command has two parts:
../project-draftis the new folder, created next to the project folder.draftis the branch to open in it.
Now the new folder has the files of draft:
ls ../project-draftnotes.md
report.mdThe original folder is still on main. We can open project-draft in the file manager, in an editor or in another terminal, and keep both in view.
Create the new folder outside the project folder, as ../ does here. A folder inside the project would show up as untracked content in the first branch.
To list all the folders and their branches:
git worktree list/home/user/project b2a07bd [main]
/home/user/project-draft 4f9ea19 [draft]Edit and commit
A commit made in a folder goes to that folder’s branch. Let’s add a line to notes.md:
cd ../project-draft
echo 'More text.' >> notes.md
git add notes.md
git commit -m "Extend notes"[draft 999734f] Extend notes
1 file changed, 1 insertion(+)The [draft ...] part confirms where the commit went. There is no need to copy anything back: both folders share one repository, so main’s folder can already see the commit.
cd ../project
git log --oneline draft999734f Extend notes
4f9ea19 Add notes
b2a07bd Add reportIf the project has a remote, we push from either folder. The first push of a new branch needs its name:
git push -u origin draftThings to know
One branch, one folder
A branch can be checked out in only one folder at a time. Trying to switch to draft in the first folder now fails:
git switch draftfatal: 'draft' is already checked out at '/home/user/project-draft'This is helpful: it stops two folders from changing the same branch behind each other’s back.
Only committed files appear
The new folder is filled from the branch’s commits. Uncommitted changes, untracked files and ignored files stay in the original folder. If a script needs an ignored file, such as a local settings file, we copy it over ourselves.
It is not a second clone
The new folder holds no copy of the history. Its .git is a small file that points back to the main repository:
cat ../project-draft/.gitgitdir: /home/user/project/.git/worktrees/project-draftSo we should keep the main project folder where it is.
Create a new branch and its folder together
The -b option makes a new branch, starting from the current one:
git worktree add -b experiment ../project-experimentPreparing worktree (new branch 'experiment')
HEAD is now at b2a07bd Add reportRemove the folder
When we have finished, we remove the folder with Git:
git worktree remove ../project-experimentIf the folder has changes or new files that are not committed, Git refuses, so that we do not lose them:
fatal: '../project-experiment' contains modified or untracked files, use --force to delete itWe commit or delete those files and try again. Removing the folder keeps the branch and its commits.
If we have already deleted a folder in the file manager, Git still remembers it. This clears the record:
git worktree pruneSwitch or worktree?
| Task | Better choice |
|---|---|
| A quick look at another branch, with nothing unfinished | git switch |
| Browsing two branches side by side | git worktree |
| Urgent fix while the current work is half done | git worktree |
| Comparing the output of two versions | git worktree |
Quick reference
git worktree add ../folder branch # open an existing branch
git worktree add -b newbranch ../folder # new branch in a new folder
git worktree list # show folders and branches
git worktree remove ../folder # remove a folder
git worktree prune # forget deleted folders