Using Git from RStudio
How to navigate Github repositories through RStudio. Helpful if you're writing a paper as an RMarkdown or if you're sharing an RProject with others.
Last updated
How to navigate Github repositories through RStudio. Helpful if you're writing a paper as an RMarkdown or if you're sharing an RProject with others.
Last updated
Here is an R project saved locally, with a .csv file, a .rmd file, and two .r files. I want to create a github repository with all the items in the R project folder.
Go to github website:
Click the New repository
button.
Set your new repository name, and click Create repository
button.
Copy the github repository URL from the Quick setup
chunk.
Open your local R project in RStudio:
click Tools
-> Shell…
.
Type the following commands in the pop-up window:
git init
git add .
git commit -m “first commit”
git remote add origin
the URL you pasted from github website
git remote -v
git push origin master
If you return to Github, all of the items in your local R project folder should be in your repository with the message "first commit".
Go to the web link for the GitHub repository you want to work with. Click Clone or download
, and copy the URL.
Open RStudio and File
-> New Project
-> Version Control
-> Git
. Paste the URL copied from GitHub to the Repository URL
, and click Create Project
.
After you've created the project, you should be able to open and run the code.
After making changes, and before pushing them to the remote github repo, you need to commit them.
Save the file(s) -> click the Git
tab on the right panel -> select the file(s) you want to commit -> clickCommit
-> make sure the Staged
button is selected -> add your comment -> and click Commit
.
At this stage, everything is still in your local. To upload your modifications (i.e., your commits) to GitHub so that others can see and pull, you need to push. You can do this by click the Push
tab. The Push
tab on the right panel and the Push
tab on the popup "commit" window are the same. Note: only "commited" changes will be uploaded to GitHub.
If you are working with others on a project, you'll need to Pull
their changes to your local copy. Use the down arrow to Pull. This should be the first step you take when you open a shared project. You should also Pull before you Push if you think others have been working on the project.
If you hit an error when trying to push your changes ("Updates were rejected because the remote contains work that you do not have locally"), never fear! Merge conflicts may occur if competing changes are made to the same line of a file or when a file is deleted that another person is attempting to edit. You just have to manually fix the conflicts and you'll be good to go.
Pull the remote changes. This will fail with a conflict error, but now your file will have both your and the other person's modifications.
2. Open the .Rmd and search for "HEAD". This will bring you to the conflict(s), which will look something like this:
3. Fix the conflict by editing the file as needed. Look over the two versions and decide which is better, or combine the two appropriately. Remove any incorrect text and conflict markers until what's left is ready to knit.
4. Knit. (This will overwrite the conflicting .pdf and .tex; you do not have to fix those conflicts manually.)
5. Look over everything to make sure you've fixed all the issues, then stage, commit, and push!
The git pane will show the status like this: