Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've started using worktrees recently and I have nothing but praise for it. It's especially useful to me because I work on multiple features and want to reduce friction from context switching. I basically have a structure like `/worktrees/<project>/<worktree>`. I use it alongside direnv and have my .envrc in the top-level project. That essentially allows me to set up project-specific environments for all of my worktrees. This works neatly with emacs projectile mode and lets me switch between different projects/features seamlessly. My head feels a lot lighter not having to worry about my git branch state, stashing changes, and all that jazz. I think it's a great tool to have in your repertoire and to use depending on your needs.


this is what I do as well but my structure is `~/git/<project>/<worktree>` for the worktrees and a bare git repo is stored in `~/git/<project>/.git`.

The workflow I do is

1. git clone https://path.to/repo.git

2. mv repo/.git repo.git && rm -rf ./repo && mkdir repo && mv repo.git repo/.git && cd repo

3. git config --bool core.bare true

Now you can just create worktrees in the root of the git repo.

This has the advantage that the `~/git/<project>` directory is still understood as root of the git repo and the worktrees are never checked out inside the working tree directory.


Why not `git clone --bare`?


I've tried this and it seems to be working fine.

git clone --bare https://path.to/repo.git

mv repo.git .git

now we can create worktrees in the same folder.


fwiw you can actually one line that

> git clone --bare https://path.to/repo.git .git


You can bare clone and it works fine. This is in part just habit from converting existing repos into bare + worktree repos.


The only problem I've ever had with work trees is my own lack of discipline; even with one directory per branch I seem to switch branches within each checkout without thinking and so make me life needlessly hard>:( Perhaps I should write a shell function to outright disallow switching branches...


I do that to but for the many stable branches of OpenStack/Ceph/etc that I’m working on at once (working on the distro/backporting side often back porting patches 3-8 releases). It’s great.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: