r/ProgrammerTIL • u/cdrini • Apr 18 '21
Other Language [git] TIL about git worktrees
This one is a little difficult to explain! TIL about the git worktree command: https://git-scm.com/docs/git-worktree
These commands let you have multiple copies of the same repo checked out. Eg:
cd my-repo
git checkout master
# Check out a specific branch, "master-v5", into ../my-repo-v5
# Note my-repo/ is still on master! And you can make commits/etc
# in there as well.
git worktree add ../my-repo-v5 master-v5
# Go make some change to the master-v5 branch in its own work tree
# independently
cd ../my-repo-v5
npm i # need to npm i (or equivalent) for each worktree
# Make changes, commits, pushes, etc. as per usual
# Remove the worktree once no longer needed
cd ../my-repo
git worktree remove my-repo-v5Thoughts on usefulness:
Sooo.... is this something that should replace branches? Seems like a strong no for me. It creates a copy of the repo; for larger repos you might not be able to do this at all. But, for longer lived branches, like major version updates or big feature changes, having everything stick around independently seems really useful. And unlike doing another git clone, worktrees share .git dirs (ie git history), which makes them faster and use less space.
Another caveat is that things like node_modules, git submodules, venvs, etc will have to be re-installed for each worktree (or shared somehow). This is preferable because it creates isolated environments, but slower.
Overall, I'm not sure; I'm debating using ~3 worktrees for some of my current repos; one for my main development; one for reviewing; and one or two for any large feature branches or version updates.
Does anyone use worktrees? How do you use them?
- Docs: https://git-scm.com/docs/git-worktree
- Cool YouTube video where I learned about worktrees: https://www.youtube.com/watch?v=2uEqYw-N8uE
15
u/18randomcharacters Apr 19 '21
Wait, so would this let me be working on a large change for my project, with many commits and many unstaged/committed changes...
And then switch to a new clean branch (work tree?) To do a quick hot fix? I always have to stash huge change sets to do that.
6
u/HighRelevancy Apr 19 '21
This whole conversation is a bit nonsense, specifically with regards to the worktree vs branches thing. If you read the doco...
The whole thing here is just that you get a second working directory to play with. It's basically like clone-ing it a second time, except they're actually the same underlying git objects. The benefits compared to a second clone would be things like
That video fucking kills me too. Dev pop-culture is still awful I see. The emotionally balanced reaction to this isn't "oh my goodnessssssss" and an expression of gleeful constipation, it's just "well that makes sense, two clones is kinda clunky, of course this feature exists".
8
u/iTZAvishay Apr 18 '21
I'm mostly confused about why you'd have a branch named
master-v55
u/sim642 Apr 19 '21
No, it requires each worktree to be on a different branch, so you need the branches anyway.
3
u/bacondev Apr 19 '21 edited Apr 19 '21
For a large project, I used git hooks to use worktrees under the hood when checking out a branch. Or maybe it was a custom command and not a git hook. I can't remember. Instead of replacing the current branch with soon-to-be checked out branch, it'd create a new directory for the soon-to-be checked out branch (if one didn't already exist) and update a symbolic link to point to that directory. That way, I had all the advantages of worktrees while keeping my IDE sane. Because I used the symbolic link, I was always working in the “same” directory. And this was all done in a way that didn't affect my coworker's workflow. This was four or five years ago and I no longer have access to the codebase, so unfortunately my memory is a bit too hazy on it. I would only recommend bothering with this if you're often switching between unfinished branches.
15
u/Ramipro Apr 18 '21
This is ... surprisingly useful. Thanks for the tip!
7
u/botle Apr 18 '21 edited Apr 18 '21
I don't get it. What are the benefits over simple branches?
3
u/itoshkov Apr 19 '21
Worktree is an alternative to stash. Let's say you're working on a feature and you need to check or fix something in another branch. You can fit stash your changes, switch to the other branch, do what you need to do there, switch back to the feature branch and pop the stash back.
This works, but there are some problems. For example, you might have some changes staged, and have others not staged. When you stash and unstash them, all the changes are unstaged. Or you might have a new file, which you don't want to add to git yet. Staging won't touch it, which is good, but your code might not compile when you change to the other branch.
With worktree you open the other branch in a new folder. The current folder is untouched, so you can even work on both simultaneously. When you're done, you remove the folder and delete the worktree. It's relatively cheap as you don't copy nor download the git history.
One irritation with worktree might come from your IDE. You may need to register and reindex the new folder.
2
u/quasarj Apr 19 '21
Wow! I make a second copy of my main repo frequently, I’ll try this next time! Thanks!
2
u/wesleyoldaker Jul 24 '26
I am going to be using it (for the first time) for an A/B test scenario. I need to have two different copies of the same sets of databases, one for A and one for B, and I need to have a git worktree to move between the code which produces the A data and the code that produces the B data. The A branch is much older than the B branch.
I'll comment about how it went, but I saw no cleaner way to do it than using git worktrees.
1
u/EmmaKayAlexandra Feb 10 '26
Commenting here to preserve my place in history for the git feature that finished off human programmers
0
u/pablo1107 Apr 19 '21
I just started experimenting with this on my dotfiles repo. I'll have one branch per program, let's see how it goes. haha