Is there a way to detach and attach git worktrees? Basically, I'd like to have the following workflow:
- Create a worktree
- Detach work tree after doing some work (adding commits), the directory should not get deleted
- checkout worktree branch in my main checkout
- do some push/linting/pushing to company server
- reattach worktree, and continue working on existing directory if necessary
To explain my reasoning behind my workflow... I have a bunch of settings which are tied into the path of the of the main checkout repository... this includes linting/pre-commit checks and some other workflows (the setup is complicated, and I am unaware of the details)... what I'd ideally like to do is the following switch back and forth from the git worktree to the main checkout location of the repo.... similar to how you would switch between branches.
e.g. I do the following
- create worktree branch A in dir A
- do some edits
- "unlock" worktree A
- checkout branch A branch in main checkout directory
- do linting/pre-check operation (these do not edit any file in the branch)
- checkout master branch in main checkout directory
- go back to editing worktree A in dir A
The
locksubcommand (git worktree lock) exists specifically to keep the main repository from considering the worktree "gone" if the added work-tree is on, e.g., a thumb drive that is not always mounted. The use case here is:which is all on one machine with one main repository. It's not clear if this is what you meant, or if you meant:
originrepoThis could work, but has some issues. First, there is no formal way to tell Git: "this work-tree that I never created for this repository suddenly exists now, please add it". However, if you used
git worktree addto create a work-tree in the place that the thumb drive work-tree will be, and then mount the thumb drive over that area (perhaps after removing the work-tree itself without Git knowing), that will work, under a constraint or two.Specifically, when you're in an added work-tree:
HEADand index files for this work-tree actually live inside the main repository.git adda file to copy it to the index, not only is the index itself in the main repository updated, the underlying Git object goes into the main repository as well.Hence you'd have to also bring over the contents of those files—the per-worktree
HEADand index, plus the underlying Git object files—if you ever change them from the contents they have right aftergit worktree add. If not, thegit worktree addyou do on machine-X, once, to set things up before you swap in the thumb drive work-tree, will suffice.(Note that if you do run
git addor modifyHEAD, the underlying repository objects are not safe in Git versions through 2.15.0, where this was fixed. Essentially, the main worktree can gc them after the default prune expiration, typically 14 days. The short version of this warning amounts to "don't keep added worktrees in use for more than two weeks", though that's a bit of an over-generalization. Slightly related: renaming branches that might be in added worktrees is buggy in Git versions predating 2.15.0.)