Site icon R-bloggers

Load different R package versions at once with git worktree

[This article was first published on Maëlle's R blog on Maëlle Salmon's personal website, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Do you ever see GitHub issue comments where someone posts the results of a reprex with a current package version, and then with an older one, to prove a regression? How would you go about preparing such a report? Today I learnt there is a clean way to have different versions of a codebase at once on your computer, thanks to the ever powerful Git.

Assumption: you want to load the package at different stages of its history

I’m not talking about installing different R package versions at once, this is not a post about renv or Docker. 😉 I mean having the ability to quickly run devtools::test() or devtools::test_active_file() on two different versions of an R package source.

Although I guess that for running an actual reprex::reprex() on your code, you’d need a quick installation in each version of the codebase.

Solution: git worktree!

The git worktree command tends to be introduced as an alternative to a quick git stash when switching from feature work to a hot fix (well, in all three posts I read/skimmed on the topic 😅). With git worktree you can create a new folder somewhere on your computer, that is at a different Git state than your current folder (other commit/branch/release – aren’t all of those the same thing anyway 🤪).

Say, in Documents/rigraph you are working on the main branch of the https://github.com/igraph/rigraph repository. Now, someone asks you “what did this code do with version 1.6.0”.

Past me would have diligently installed that version of the package in the same R session, probably from CRAN or with pak, run the code, re-started my R session, re-loaded the development version or something like that.

Current me would run git worktree add ../rigraphv1.6.0 v1.6.0 which means I now have a folder Documents/rigraphv1.6.0 with the rigraph repository as it was at the v1.6.0 release. Then inside that folder I can open my favorite IDE, load rigraph as it was at the time, and run the code I am curious about. I can switch between my two IDE windows to try things out with the two package versions.

Once I am done, from my original rigraph folder I can run git worktree remove rigraphv1.6.0 (or git worktree remove rigraphv1.6.0 --force if there are some changes in that folder). git worktree list shows me what worktrees (folders!) there are.

Conclusion: git worktree is great!

I’m glad I learnt about git worktree, “Manage multiple working trees” a.k.a (in my head) “Create multiple folders corresponding to a single Git repo at different states”. Do you use git worktree? Did I miss something in the around 10 minutes I’ve known about it? 😅

To leave a comment for the author, please follow the link and comment on their blog: Maëlle's R blog on Maëlle Salmon's personal website.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version