3

For example, I have /tmp/foo Git repository, etc branch and non-empty /etc directory. I wish to attach the /etc directory as a worktree to the etc branch. git worktree add /etc etc won't work due to "already exists".

I saw some old discussion in between Git developers but not sure was this case/issue solved nowadays.

I understand that I can exec something like the following:

  • git worktree add /tmp/bar etc
  • Rename /tmp/foo/.git/worktrees/bar to /tmp/foo/.git/worktrees/etc
  • Edit /tmp/foo/.git/worktrees/etc/gitdir
  • Create proper /etc/.git file which pointing to /tmp/foo/.git/worktrees/etc

But I am wondering is there some more handy way to do that in the modern Git?

crazyh
  • 325
  • 2
  • 10

1 Answers1

1

git worktree (for supporting multiple working trees) is not that old (July 2015 when first introduced)

But it requires indeed an empty folder.
Which means an "easier" workaround would be to rename the target folder, create the worktree with the right name, then replace the new folder content by the renamed one.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yeah, thanks. It probably can be a solution too but it will not work with RO-directories because I can not temporary rename system directories. I want to use Git worktree (instead `etckeeper`) to track an existing system directory (or even whole root FS). – crazyh Jan 24 '20 at 17:12
  • @crazyh `git worktree` does not track existing folder (system or not). It merely checks out a branch of a repository in a separate folder. – VonC Jan 24 '20 at 22:16