1

When I went through many tutorials related to git worktree, everywhere in the command which was used to demonstrate the creation of worktree, .. was used for the path where the working tree should be created - which indicates that the new working tree directory to be created at the same directory hierarchy level as that of the git repo.

An example taken from the git website

git worktree add -b emergency-fix ../temp master

And another from Atomic Object-

git worktree add ../new-worktree-dir some-existing-branch

So I am wondering is there a good reason to create worktree at the same directory hierarchy as the git repo?

Technically, you can create the working tree inside the git repo also. Git allows it.

And I feel creating all the worktree belonging to a particular git repo under the git repo directory would give a logical grouping.

git worktree add -b emergency-fix ./temp master
Darshan L
  • 824
  • 8
  • 30
  • If the git repository is non-bare then adding a worktree inside of it can become confusing (I don't know if git knows how to handle it, but even if it does, it's confusing to users). For a bare repository, I see no reason not to create the worktrees inside of it. – Joachim Sauer Oct 21 '20 at 07:50
  • In a non-bare repository, the repository itself is also a worktree. If you create extra worktrees inside it, you need to carefully exclude the worktrees to avoid adding them by mistake. A nested worktree could be confusing with a submodule. – ElpieKay Oct 21 '20 at 08:02
  • Got it. So how can I create a worktree out of bare git repo? A question related to that has been posted here - https://stackoverflow.com/questions/64457757/how-to-create-a-git-repo-without-a-working-directory?noredirect=1#comment113978755_64457757 Seems you know on usage of worktree with bare git repo, can you answer the above SO question? – Darshan L Oct 21 '20 at 08:10
  • 1
    @DarshanL A bare git repo is designed as a repo without worktrees. Although it's possible to create worktrees for a bare repo, it's recommended to clone a non-bare repository with `-n`. `-n` instructs the repo not to checkout any files, so that its main worktree is empty. And then you can create extra worktrees for refs and revisions. – ElpieKay Oct 21 '20 at 08:20
  • @ElpieKay wow... `-n` option with `git clone` works. This is what I was looking for. Great! Thanks. You can answer on that other question, I can accept your answer. – Darshan L Oct 21 '20 at 09:06

0 Answers0