Search code examples
gitgit-worktreegit-maintenance

git maintenance with worktrees


Let's say I have a repository and have multiple worktrees,

e.g.:

git clone https://github.com/octocat/Spoon-Knife
cd Spoon-Knife\
git worktree add -b myfix ../myfix main
git worktree add -b myfix2 ../myfix2 main

Let's imagine Spoon-Knife is a really big repository and I want git to take care of speeding things up. Do I need to call git maintenance start on every worktree or is it just fine to call it once on the folder Spoon-Knife?

I am suspecting I only need it once but if I call it on other worktrees git doesn't complain either.


Solution

  • Only once.

    All Git worktrees share the same repository. They just link to it (with the exception of the main worktree where it resides). But each worktree has a few things of its own:

    • The index
    • HEAD
    • Pseudorefs like MERGE_HEAD
    • Per-worktree configuration files
    • Per-worktree refs

    git-maintenance(1) does maintenance on the repository. For example the garbage collection (git-gc(1)) optimizes the object layout and removes objects that are unreachable. We can infer that garbage collection takes into account absolutely all refs including per-worktree refs, since if it didn’t running it would be dangerous. And since it takes into account all refs we can infer that it cleans up things relative to each worktree.

    All the other per-worktree metadata are either vital (the index) or just a small handful of files that keep state (like MERGE_HEAD).

    So yes, only once is enough. This is in fact one of the direct benefits of using worktrees instead of multiple clones.