Getting more comfortable with Git and GitHub in RStudio

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.

Lisa Lendway , Heather Lendway
02-24-2021

Thank you

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.

Who is this for?

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.

What you should already know

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.

Review setting up the repo and cloning it

In the first part of the video, we go through the following steps:

  1. 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.

  2. I clone the repo from GitHub to my computer in RStudio, which also creates a an R Project and a folder on my computer.

  3. 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:

Add a collaborator and they clone the repo

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.

  1. I add a collaborator, Heather.

  2. Heather clones the repo from GitHub to her computer in RStudio.

Watch through minute 7:41:

Do some committing, pushing, and pulling

Now we show how to work together on the project. Good reminder to remember your passwords.

  1. I make a change to the .Rmd file, commit, and push it to GitHub.

  2. Heather pulls my changes from GitHub to her computer, makes a new change, commits, and pushes to GitHub.

  3. I pull in the change from GitHub.

Watch through minute 14:00:

Communication breaks down

  1. In the next portion of the video, we create a merge conflict and show how to resolve it. Phew, that wasn’t so bad. Remember, communication is a key part to using GitHub collaboratively!

Watch through minute 22:06:

Adding branches

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:

  1. I add a branch in by clicking on the “branch” in the Git tab (next to settings).

  2. 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.)

  3. 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:

Add an issue and solve it

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.

  1. I show creating an issue and using branching/PR (PR is short for pull request) to solve issue and close issue.

Watch through minute 36:28

I use the terminal đŸ˜±, and come out alive!

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:

  1. 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.

  2. 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.)

  3. 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.

  4. 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!:

Bonus information

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.

Citation

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}
}