I’m still mad I haven’t found this out before.
Imagine you’re working on something, then you need to work on something else on the same repo for whatever reason - you have some modified files, some new ones, some deleted… what do you do?
I would usually stash them without a description, because, you know, I’m in a hurry, and then spend a lot of time later finding out which stash it was. Or… I just switch to a new branch and
git commit -am wip.
What if I told you there’s a better way?
Introducing… git workstrees! A feature that has been there forever and yet I don’t think I know anyone who ever used it!
The main change in your workflow is that you’ll need to make a bare clone of the repo you want to work on, e.g.:
git clone --bare email@example.com:goreleaser/goreleaser # or, with gh gh repo clone goreleaser/goreleaser -- --bare
Then, you can create a new worktree for the main branch:
git worktree add main
And then you
cd into the
cd main git show @
Boom! You now have a folder for the main branch… you can update it and do whatever you would as a normal git repository.
Now, let’s say you need to work on something else:
cd .. git worktree add not-main cd not-main git status
There you go, a clean copy of the
main branch! And the previous edited
main still lives on
If you want to see my config, check out:
And this is how it looks like:
You can also add hooks to its events (e.g. switch) to auto-run things for you, for instance, setup project dependencies - or anything you’d like, really.
If you’d rather watch a video instead of reading, or if you’re someone learning vim/neovim (such as myself), I strongly recommend watching ThePrimeagen’s YouTube Channel, specially this video about git-worktree.
And that’s it! A quick tip I needed to share because its just too amazing not to.