git worktree notes
git worktree
allows you to have multiple working directories associated with one git repo. It has been useful for me to look at, copy code between, and run two different branches of my code. I learned about git worktreee from James Ide's tweet:
Have you ever needed to pause & switch branches to fix an urgent bug or check out a release branch? This all goes away with "git worktree".
— James Ide (@JI) December 1, 2016
Initial setup
Move the checked out git repository to a /main
subdirectory
$ mv /my-project /main
$ mkdir /my-project
$ mv /main /my-project
Create a new working tree from an existing branch
$ cd /my-project/main
$ git worktree add ../my-branch-working-dir my-branch
$ cd ../my-branch-working-dir
Create a new branch and new working tree
$ cd /my-project/main
$ git worktree add -b my-new-branch ../my-new-branch-working-dir origin/master
$ cd ../my-new-branch-working-dir
Delete a working tree
$ cd /my-project
$ rm -rf my-branch-working-dir
$ git worktree prune
fatal: my-branch is already checked out error
Deleting .git/worktrees/my-branch
fixed the problem for me. See https://stackoverflow.com/q/33296185/101911
Reference / See also
https://github.com/blog/2042-git-2-5-including-multiple-worktrees-and-triangular-workflowsRelated posts
- Converting an hg repository to git — posted 2014-12-03
- Git submodule notes — posted 2013-03-07
- Example using git bisect to narrow in on a commit — posted 2011-08-13
- git notes — posted 2011-08-11
- Colorized, interactive "git blame" in Emacs: vc-annotate — posted 2011-05-28