I'm using git as a version control system in RStudio. I have several different versions of an R script saved in git. Lets say, I've deleted a block of code but I've now decided I want to re-insert the code into my current R script. I know the code includes the function ddply
. This is my current workflow:
I open the Terminal in RStudio, and type:
git grep ddply $(git rev-list --all)
This brings up hundreds of lines of code, such as:
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
I sift through the code and eventually find the bit I was looking for. I copy the relevant bit of code from Terminal and paste it back into my R script. Before the code is useable, I need to delete this bit: 2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:
At the moment, this feels like a rather slow and clunky way of re-using old, version controlled code, and I might be better off using Rhistory.
Is there a faster way of getting version controlled code out of git and back into a R script in RStudio?
The RStudio interface for Git is extremely basic and does not provide a direct method to work with history beyond a simple revert
, nor with branches beyond switching.
If all you know is that ddply existed in the function you now want to reuse then your method of searching for it is really the best you can do, or perhaps the -G
grep flag for git log
. If you know anything else (ie: some sense of the time period, the file or path name, or the branch you did the work on) then you can make things much simpler by filtering the commit log with some criteria.
Once you've found the commit then you can simply interactively checkout the patch chunks using
git checkout -p
and use it just like you would if you used interactive commit. This allows for splitting the chunks to get to just the part you want. Or, if you really just want to reinsert the code using cut and paste then git show
&& copy/paste.
Don't forget that you aren't locked into RStudio's interface exclusively. There are a plethora of easy to use GUIs for git that allow for working with and searching through the history to graph explicit chunks and none of them will interfere with Rstudio. They all just work on the code and the RStudio interface will update as needed.
Git Interfaces
I'm personally partial to the terminal, the gitGUI & gitk commands, TortoiseGit, and Git Extensions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With