Search code examples
gitversion-controlgit-worktree

Long-term usage of branches with "git worktree"


I was looking into git's "new" worktree feature as it seems to fit really neatly into a problem I usually face: the need to work in multiple branches simultaneously (some of them are short-lived while others really long-lived).

I'll generally have to work for a couple of hours on release_a branch and then fix something on release_x branch and then a bit on release_h branch. As I'm lazy, what I end up doing is having multiple copies of my git repository set up with Intellij, so I don't need to be constantly switching branches here and there.

When I heard about git's worktree command I thought it was just what I was looking for -- a way to have a single repo have multiple working directories. And as a big plus, that would mean that I no longer would actually have to push stuff every time I changed something on a branch just for the sake of having another one pull it (as for instance, for an hotfix). I could just locally merge them.

Is my understanding of what git's worktree correct?

I've been playing with it but I can't really get how it works for my purposes. It seems that by default it will create the worktrees on its root folder, but that if I do a git add . those same folders will be included in the commit.

This is how I'm creating a worktree for an already existing branch my_branch (assume I'm currently @ master):

git worktree add my_branch my_branch

So my questions is twofold:

  1. Is it possible / correct to apply multiple worktrees for long-time branches, or is it just something that makes sense temporarily?
  2. How should I then properly delete my unneeded worktrees?
  3. Should I have these worktrees existing inside or outside my main git repository?

Thanks


Solution

  • And as a big plus, that would mean that I no longer would actually have to push stuff every time I changed something on a branch just for the sake of having another one pull it (as for instance, for an hotfix). I could just locally merge them.

    You can already merge the branches locally: you just have to checkout the destination branch first.
    If you have multiple local checked out folders of the same repo, yes, you would need to push/pull between them.

    With git worktree, instead of a checkout of the destination branch (to do the merge), or instead of pushing to another checked out folder of the same repo, you would need to cd /path/to/destination/branch (where it is already checked out).

    The idea is to have:

    • /path/to/your/repo (with one branch like master checked out)
    • /path/to/your/Branch1 (outside of the repo, but linked back to the repos through the git worktree text symlink mechanism)
    • /path/to/your/Branch2, ...and so on.

    Then the doc is clear:

    When you are done with a linked working tree you can simply delete it.

    The working tree’s administrative files in the repository (see "DETAILS" below) will eventually be removed automatically (see gc.worktreePruneExpire in git-config).
    Or you can run git worktree prune in the main or any linked working tree to clean up any stale administrative files.