I love git worktrees. It took me a while to change my workflow and get used to doing things differently, but it is so much better. I never have to think “oh my repo is in inconsistent state, I can’t switch my focus and start reviewing this PR, I need to first stash/commit/stage/etc. my stuff…”

I also love how Magit lets me create a worktree from a PR or an Issue (some of that is built-in, some I extended to fit my workflow). One thing is still missing though.

Cloning or starting a new repo in a practical way to be used with worktrees, for me, always requires some manual process.

First, I need to follow the recommendation I borrowed from this blogpost: How to use git worktree and in a clean way. It boils down to:

mkdir my-awesome-project
$ cd my-awesome-project
$ git clone --bare git@github.com:myname:my-awesome-project.git .bare
$ echo "gitdir: ./.bare" > .git

But when I do that, git doesn’t see remote branches anymore. So some stuff won’t work as normally expected. For that, I have to follow this advice from SO, basically adding to ./bare/config:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*

And now I’m thinking, before I try to automate this. Maybe Magit already does the right thing and I just never knew about it? Or maybe it’s much simpler than that and all that manual hocus-pocus is not needed?

How do you use worktrees with Magit? And if you don’t use worktreees, why is that? Is there even a better way?

  • github-alphapapaB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    I don’t use worktrees very often, but when I do, I just use them simply: I make a worktrees subdirectory in the repo root, then use Magit to check out worktrees into subdirectories of that.