all 8 comments

sorted by: hot top controversial new old
[–] 6 points 1 year ago (1 child)

Worktrees are great. Although I use them is a different way. I only use them to allow me to rebase or cherry pick onto branches, which require a working tree to do, without ever leaving my main branch. Basically I use them as more powerful branches then full worktrees.

Instead I just do all my work in the main branch/worktrees. When I have something I want to push to a pr I commit just that change to main. Then create a branch and work tree (I store them in .git/wt/). Then cherry pick the commits onto that branch and push that creating a pr from it. Then go back to working on main again on top of the changes I have done. Once the pr gets merged I do a pull --rebase which drops the commits from main.

This means I don't need to switch directories ever and let's me work on a feature, then do a refactor (or minor bug fix), commit and push the refactor in isolation while continuing to work on the feature on top of the refactor.

  • source
  • hideshow 1 child comment
  • [–] 4 points 1 year ago

    Glad it's there for people who need it. I've been fine with branching and stashing for 10+ years and my working directories have never been so dirty that I needed an entirely new copy of the project to do a hotfix.

  • source
  • [–] 3 points 1 year ago (2 children)

    I never really understood the advantage of worktree over having just several clone of the repos on different folders.

    Can someone explain me ? I should have missed smth.

  • source
  • hideshow 2 child comments
  • [–] 4 points 1 year ago* (1 child)

    Worktrees share the data stored in the .git folder. This saves storage (particularly on larger repos, and if you don't fetch/clone only partial history), and could allow some other workflows or safeguards (backing up just one instead of multiple, centralized local state instead of spread across different workspaces). It also means it could share repo-local git settings - like remotes, local not checked-in ignores, etc. (I assume).

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 1 year ago

    VSCode just added support too fyi

  • source
  • [–] 2 points 1 year ago

    Oh my god, I've thinking about why git doesn't operate this way for ages, but it already has for a decade. This is going to change my life. Thank you for posting this.

  • source
  • 18 comments in c/programming@programming.dev view that post
    [–] 22 points 1 year ago (4 children)

    Functionality-wise, it seems very similar to just "git cloning" another copy of the repo in a different directory (you can even use the "main" local repo as its source, and "pull" from that), which at least to me seems more intuitive to reason about and doesn't require me to learn any new commands or worry about any limitations like the issues with submodules or not being able to have the same branch checked out twice (clones don't mind).

    It's a nice idea, and I'm going to try to remember them as an option for future scenarios where they might be useful, but I think the reason they never caught on generally is just that they're not bringing anything to the table that can't already be easily accomplished by other means.

  • source
  • hideshow 4 child comments
  • [–] 16 points 1 year ago (1 child)

    Yeah, I didn't see the use for them until I got further into it over just cloning. Your worktree "folders" share the same git history and stuff. So you can stash in one and apply in the other. If you do a fetch in one, it fetches for all of them.

    Whether it's worth it for others to learn these just for those things is hard to say. I picked it up simply because I was bored and wanted to pick up a new "thing".

  • source
  • parent
  • hideshow 1 child comment
  • [–] 4 points 1 year ago

    Yeah, I think their saving grace is that they really aren't that hard to use. I don't use them often enough to remember the commands by heart, but it doesn't take me long to figure it out anew each time. Typically not as long as cloning the repo anew, at least.

  • source
  • parent
  • [–] 5 points 1 year ago*

    git worktree is just so much easier to work with if you want to work on multiple versions or branches of some code.

    It allows having multiple IDE instances open, all fully functional and indexed, and handing over commits from one worktree to another without having to fetch constantly in between.

    Trying to emulate this with multiple clones feels like trying to do OOP in C -- sure one can do it, but it's pointless hassle compared to a fleshed-out solution that works right out of the box.

    Not to mention it's so much faster and more efficient than git clone.

  • source
  • parent
  • [–] 4 points 1 year ago

    it seems very similar to just “git cloning”

    Worktrees take less space, and the fetch/pull only has to be done once. It's a nice feature but restricted to workflows where you work on a lot of features at the same time.

  • source
  • parent
  • [–] 13 points 1 year ago (6 children)

    yeah, I think these are the main hurdles for me:

    1. Untracked files are not copied

    When you create a new worktree, it is created from whatever is comitted, so gitignored or uncomitted files are not copied.

    So if you have .env files, you have to copy them over manually. And for dependencies, like for example node_modules, you would have to run npm install again in the new worktree.

    Mainly .env files, as they are handcrafted. And:

    1. Editor / IDE complexity.

    A few projects I work on are multi-root (using VS Code terminology) and that's already complex enough. Adding worktree directories means adding a level to that, which I'm not bought in. And I don't want a separate workspace for each branch I work on, that just shifts the complexity from git to the IDE / editor.

  • source
  • hideshow 6 child comments
  • [+] 6 points 1 year ago* (4 children)
  • [–] 4 points 1 year ago (3 children)

    Then we have the "it works on my machine" issue. I'm vehemently against symlinks pointing out of the code repository because of that.

  • source
  • parent
  • hideshow 3 child comments
  • [–] [S] 9 points 1 year ago (2 children)

    If they're untracked files anyway, that's unavoidable.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 3 points 1 year ago (1 child)

    If they're ignored files, setting them up locally won't end up in the repo. If you put a symlink into the repo, fixing that for your setup will register as a change within git, which can cause annoyance and even problems down the line.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 3 points 1 year ago

    Yeah, the untracked files not being copied is also a big reason why I'll typically just switch to a different branch instead.

    I mainly use worktrees when it's useful that untracked files are not copied, like when I need to check out a completely different state of the project, where cached files would need to be invalidated anyways, for example.

  • source
  • parent
  • [–] 2 points 1 year ago

    I've stopped using bare env files on the repo, I'll create an env file that populates values from a secrets manager and check this file info git. Or throw the env file info a parent dir because they're probably user specific anyway.

    Having an env file that needs to exist but isn't checked into source control creates "works on my machine" issues as well, just load them from the environment and provide a programmatic way of setting the environment (or stop pretending they're part of the project and use direnv/Mise to setup the env)

  • source
  • parent
  • load more comments (-1 replies)
    [–] 6 points 1 year ago

    I use them all the time, but that's just because of Yocto and the need to keep at least the 3 major LTS builds hot in the event something breaks.

  • source
  • [–] 5 points 1 year ago

    This is pretty cool and solves one of the problems I've had whilst playing around with things like Cursor: that it breaks my flow having to wait 5-10 minutes for it to generate code/documentation, I'd really like to use that time to focus on my main work whilst it does some grunt work. Worktrees looks like it might provide a solution to this

  • source
  • [–] 4 points 1 year ago (1 child)
  • [–] 4 points 1 year ago

    Unfortunately some developer tools fail to work correctly in separate worktrees. I used them for a while but had to give them up. For example, Maven's release plugin cannot reliably create tags / branches if you're in a separate worktree.

  • source
  • [–] 2 points 1 year ago

    Worktrees are great, and a good reason (if you needed any more) to avoid submodules like the plague. Worktrees don't work with submodules.

  • source