This blog post includes videos that review some Git and GitHub basics and build on that, showing ways you might work on a project together in RStudio.
Thanks to anyone and everyone who has ever put resources online about using Git and GitHub, especially how to use them in RStudio (see some resources below). Also, thanks to my sister, Heather Lendway, who was gracious enough to make this video with me.
Initially, I intended to make this only for my students. But, as long as I was creating it, I thoght I might as well share it with more people. Keep in mind that I am NOT a Git or GitHub expert by any means. In the video, I make some mistakes and stumble through some steps - maybe you will learn from those, too. Thereâs also a very good chance I do things in a not-so-standard way. Point those out to me, so I learn how to do this better.
I assume that you have already set up Git and GitHub on your computer. If you havenât done that yet, I have some instructions here or you can check out David Keyesâs wonderful How to Use Git/GitHub with R blog post. For more detailed instructions and my go-to resource for questions about using Git and GitHub in R, see Jenny Bryanâs Happy Git and GitHub for the useR.
In the first part of the video, we go through the following steps:
I create a repository on GitHub. Always add a README and think about a license. I forgot to mention anything about a license in the video, but itâs a good idea to have one. For information about that, see Jenny Bryan and Hadley Wickhamâs R Packages chapter on Licensing or Colin Fayâs What is a license chapter.
I clone the repo from GitHub to my computer in RStudio, which also creates a an R Project and a folder on my computer.
I save an .Rmd file to the folder/project/repository that was just created, commit the .Rmd and .Rproj file, and push changes to GitHub.
Watch through minute 3:27:
In the next section of the video, I add a collaborator, Heather! (We really are two nerdy sisters đ€ đ€.) After this, we are ready to work on our project together.
I add a collaborator, Heather.
Heather clones the repo from GitHub to her computer in RStudio.
Watch through minute 7:41:
Now we show how to work together on the project. Good reminder to remember your passwords.
I make a change to the .Rmd file, commit, and push it to GitHub.
Heather pulls my changes from GitHub to her computer, makes a new change, commits, and pushes to GitHub.
I pull in the change from GitHub.
Watch through minute 14:00:
Watch through minute 22:06:
In this part of the video, we explore adding branches as a way to hopefully avoid so many merge conflicts. Doing this still involves communication, especially as we merge changes back to the main branch. Here are the steps we take:
I add a branch in by clicking on the âbranchâ in the Git tab (next to settings).
I make a change to the .Rmd file in the new branch and commit and push. Heather pulls those changes and sees the new branch. (We didnât actually know for sure what was going to happen but were happy with this.)
In GitHub I merge the changes via a pull request, delete the branch, and Heather and I both pull in the changes from GitHub.
Watch through minute 30:30:
At this point in the video, we say good-bye to Heather âčïž, and Iâm left to fend for myself. I fumble a bit. Also, I really donât have a clue if I did this in a ârightâ way. Someone should tell me if I didnât.
Watch through minute 36:28
In this part of the video, I illustrate how to go back to previous versions of files depending on what you have done. Most require use of the terminal, which I am still very uncomfortable with. Specifically, I do the following:
I saved a file but donât want the changes I just made. I right click on the file and choose revert. You could also choose diff to see how it has changed from its last version.
Oh shoot (or maybe a worse word), I didnât mean to commit that! (or I canât push what I just committed). Now I need to move to the terminal. When there, I use git reset HEAD~1
. to bring back all the things I committed. I could now delete them if I really donât want them at all or edit at will and recommit. You also get a bonus mistake here because I forgot that I was in a branch đ. I delete the branch and make the change again on the main branch. (My child also makes an awesome cameo.)
I made another mistake! But now Iâve pushed it! How do I fix this? We can go back to a previous commit by using git revert <SHA>
in the terminal, where <SHA>
is the alpha numeric string that defines that commit. This actually adds another commit (see this resource), which I forgot about while recording the video. You may end up with a merge conflict, which you will need to resolve (Iâd recommend doing that inside of R Studio) and if you end up in a place with a bunch of ####
symbols in the terminal, type :q
to get out.
When in doubt, do as Jenny Bryan advises and âBurn it all downâ. Weâre all human. We all make mistakes. Itâll be ok.
Watch to the end!:
You might have noticed that I used emojis in this document. Thank you, Hadley Wickham, for the emo
package! I also looked up their names in the unicode emoji list - remember to put underscores between words.
For attribution, please cite this work as
Lendway & Lendway (2021, Feb. 24). Lisa Lendway: Getting more comfortable with Git and GitHub in RStudio. Retrieved from https://lisalendway.netlify.app/posts/2021-02-24-gitinrstudio/
BibTeX citation
@misc{lendway2021getting, author = {Lendway, Lisa and Lendway, Heather}, title = {Lisa Lendway: Getting more comfortable with Git and GitHub in RStudio}, url = {https://lisalendway.netlify.app/posts/2021-02-24-gitinrstudio/}, year = {2021} }