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
View all comments
Show parent comment
2
u/HighRelevancy Apr 20 '21
Dev culture and dev pop-culture are not the same. I don't know how else to put it. It's like, actual graphic designers versus dudes who just got photoshop and are making youtube videos on "oh my god guyyssssss I just found the most exciting new feature, it's called the paint brush, like and subscribe".
Or the non-stop circlejerk about things that people don't even know about, like this thread on new C++20 features where people are complaining about "oh the complexity, this is unbelievable"... how many of those people do you think ACTUALLY write C++ for a living, or even do significant hobby work with it, or do any sort of professional dev work at all? Or all the threads and YouTube channels chock-full of people comparing the compilation speeds of Rust vs C++, as if that's actually the deciding factor of what language you're going to use for a project?
I'm into photography and it's got the same problem too. Endless channels full of comparisons between dozens of cameras (which usually come down to "more money + newer tech = better, give or take a little bit", no surprises there), as though any given individual isn't going to just buy one camera that fits their needs and use it for ages. Why don't these people go actually make something with these cameras?
Gear Doesn't Matter by Sam Newton