I am NOT a Git/Github expert!
I am NOT a Git/Github expert!
Git/Github can help us with this! How many of you already use Git/Github?
I hope to give you a comfortable format to try things and learn. Please, ask questions and interrupt when I do something too quickly or that you don’t fully follow. There are no stupid questions!
If you don’t have these libraries installed already, you can start doing that now. You will definitely need the first 2 libraries. The others may not be necessary.
library(usethis) # for nice interactions with Git/Github library(gitcreds) # for functions that help set up a PAT library(credentials) library(gert) library(gh)
Created an account on https://github.com/.
I may not need to tell you this, but choose a “nice” username. Something that you often use and that incorporates part of your name. Definitely something you will be ok revealing to a future boss.
Check to see if you already have Git installed by typing which git
in the shell/terminal.
If this returns something like /usr/bin/git
then you are finished and don’t need to install Git. On a Windows machine, you may not even be able to type the which git
command successfully.
Figure: Results of running which git in the terminal on my computer
On a Windows:
On a Mac:
Enter either git --version
OR git config
in the terminal/shell to elicit an offer to install developer command line tools. Accept the offer … click on install.
Some of you on a Mac may need to do the following in the terminal if you try to open a project unsuccessfully. You’ll find out if this is the case in a moment.
xcode-select --install
If you haven’t already, you’ll need to install the usethis
library and load it. Run the following code in the console with some minor changes. The user.name
is your Git username. This can be different from your GitHub username, although it might be a good idea to just keep it the same. The user.email
MUST be the same as your GitHub user email.
use_git_config(user.name = "Jane Doe", user.email = "jane@example.org")
A PAT, or Personal Access Token, is now necessary in order for RStudio and GitHub to talk to one another.
create_github_token()
function from the usethis
library in the console. This will take you to the GitHub website where you can create a token - store that somewhere safe and don’t lose it! You’ll need it in a moment and maybe sometime in the distant future.gitcreds
package in RStudio and load the library in the console.gitcreds_set()
function in the console. If you are given options, choose the option to Replace these credentials and then paste in your PAT that you just created in the previous step.Find detailed images of this process here.
Create a repo on https://github.com/. After you login, you should see a little icon in the upper right-hand corner. Mine is an image of me. If I click on that a drop-down appears and I can choose “Your repositories”. Do that.
Click on the “New” button. Name your repository. Choose Public and check the box next to Add a README filend probably add a license (I usually choose MIT but do not trust me to explain any of the legality of this). Then click Create repository.
Clone the repo by clicking the green code button and copying the link. In R Studio, do File –> New Project –> Version Control –> Git –> Paste the link you copied and put it in the desired location. Click Create Project. This will open a new RStudio session. You should now see a Git tab (in upper right-hand corner by default) and there should be a .Rproj file and .gitignore file there.
We will do these steps together!
I don’t like the terminal, but sometimes I’m forced to use it. When I have to do that, my palms get sweaty, and I sometimes close my eyes and peak through my fingers after hitting return while I wait in fear to see what sort of mistake I might have made. I’ve learned a few Git commands that I feel comfortable enough to share.
There’s a video of me doing these same things here: https://lisalendway.netlify.app/posts/2021-02-24-gitinrstudio/#i-use-the-terminal-and-come-out-alive
This first one doesn’t actually require the terminal (yay!).
You save a file, but don’t want those changes. You can right-click on the file in the Git tab and choose revert. You can also choose diff to see how it has changed from its last version. This will change the file back to the previous commit. So, if you’ve saved a lot and not committed, that could be a lot of changes.
You committed something you didn’t actually want to commit. Or maybe you committed something that’s too large to push to Github (that’s usually my mistake). So, I need to “un-commit”, which means heading to the terminal. In the terminal, run git reset HEAD~1
which will bring back all the files you just committed as if they were never committed - you may need to refresh the Git pane.
Then, you can make the changes you want, even deleting a file, and re-commit.
This time you committed and pushed. You may have made additional commits that you DO NOT CARE ABOUT. To revert to an old commit, use git revert --no-commit <SHA>..HEAD
in the terminal, where <SHA>
is the alpha numeric string that defines that commit. You can find the <SHA>
in the history (the clock icon) in the Git tab.
This time you committed something, pushed it, and made more commits along the way.
We can go back to a previous commit and keep our new commits by using git revert <SHA>
in the terminal. This will likely lead to a merge conflict (which you can resolve in RStudio) or put you in the VIM window (I don’t even know what that means) where you should push :q
as quickly as possible to escape.
Now, let’s explore how we can work with others on a project by adding a collaborator.
Let’s do this together! Would someone volunteer to be my collaborator?
Once we have our collaborator(s), we’ll add some steps to our usual process.
Which brings us to ….
If you and a collaborator are working on a file at the same time, you’ll likely run into a merge conflict at some point. This means you’ve tried to push out new work, but in the mean time a collaborator has already pushed out more new work that you hadn’t pulled in. Sometimes Git can resolve them on its own but often it can’t.
When that happens, you need to resolve it by editing the file where the merge conflict occurred. You can see details of how to do that here: https://github.com/llendway/github_for_collaboration/blob/master/github_for_collaboration.md#merge-conflicts
Let’s create a merge conflict together!
Once you’re comfortable using these tools on your own and with some trusted collaborators, you may want to start making suggestions to other people’s code - maybe add a feature in your favorite package or even just suggesting a typo fix in some documentation. That’s a pull request!
We are going to use the instructions here: https://usethis.r-lib.org/articles/articles/pr-functions.html
And we’re going to make pull requests to a PR practice repo I created: https://github.com/llendway/pr_practice
We will need to load the usethis library. You should also have the gert, credentials, and gh packages installed. (If anyone gets an error like this: “Error in validate_gh_pat(new_gh_pat(x)) : A GitHub PAT must consist of 40 hexadecimal digits”, update the gh package or reinstall, see this post).
library(usethis)
You are going to fork and clone the repo. Forking is putting it in your own Github repo and cloning it puts it on your computer. This function also sets the upstream remote to the original repo. This example uses my “other” GitHub account, which is pretty much a garbage account for me to practice with.
create_from_github("proflendway/test_pr")
You can change the default location, using destdir
argument. If you are making a PR to your own repo, it won’t fork it (since you already own it).
After you run the code, you will see something like this, and I new RStudio window will open with this project open.
Next, we create a branch so changes we make take place outside of the main branch. In the pr_init()
function, name your branch. Do this in the project that was just opened - you may need to reload the usethis library first.
pr_init(branch = "expand_readme")
It may take a second for the new branch to show up in RStudio. Make sure you are in that new branch (it says you are but double check). Make a small change, save, and commit the file.
Once you’re finished making changes, use the pr_push()
function (no arguments) to push out the changes to your copy of the repo on github. That will open a Github page and sets you up to make a PR!
After creating the pull request, you can (and should!) write a message about the changes you are proposing.
They could merge in Github, or fancier things.
Let’s do it!
We’re going to make pull requests to a PR practice repo I created: https://github.com/llendway/pr_practice
BURN IT ALL DOWN! No, really, you don’t have to feel badly using this as a strategy. https://happygitwithr.com/burn.html