Connect an existing RStudio project to GitHub
A nice feature in RStudio is that it seamlessly integrates version control via Git and GitHub. Git is a version control system keeps track of changes to your code. GitHub is an online hosting service that facilitates collaborating on code based projects. In this post, I will go through the steps of connecting an existing RStudio project to GitHub.com. The only requirements are that you have a pre-existing GitHub account and have Git and Git Bash installed on your local machine.
In RStudio…
- Open the project that you would like have associated with a GitHub Repository
- Select Tools from the menu bar, then click on Version control, and then Project Setup
- In the Project Options window, select Git/SVN, and then select Git as the version control system in the drop-down menu
- After selecting Git, a pop-up window will ask if you want to initialize a new git repository.
- Select Yes, and then select Yes to restart the R Session
- Upon restart, a new Git` tab will be available in the Environment pane
- Under the Git tab, select the check boxes of the files to upload to a GitHub repository
- Select the Commit button, type in a Commit message, select the Commit button, and then Close the new window
On Github…
- Sign in to your account and navigate to your Repositories page
- Then select the green New button, to create a new repository.
- Type in a new repository name and select public or private, then select the green Create repository button.
- Verify that “Add README” is not selected as this changes how to set up the repository
- From here, copy the three lines of code from the section titled “… or push existing repository from the command line”
Back to RStudio…
Linux
- On a linux machine, you should be able to go back to RStudio, and open a new terminal window
- Go to Tools in the menu bar, select Terminal, and then New Terminal
- Then paste in the three commands from the GitHub page, into the terminal window and press enter
Windows
- On a windows machine, set RStudio to open Git Bash terminals instead of Command Prompt or Windows Powershell
- Go to Tools in the menu bar and select Terminal Options
- Select Git Bash from the drop down menu, select Apply, then OK
- You can then open a new terminal window by going to Tools, Terminal, New Terminal,
- In the new terminal window, paste in the three commands from the GitHub page and press enter
Re-authorize OAuth Application ‘Git Credential Manager’
- If you run into an issue with this error,
- Open Credential Manager in Windows (type it into the task bar search field)
- Delete the existing credential for github.com
- Re-initialize the credential manager in Git Bash then run git pull.
- Follow the instructions to authenticate with SSO
- Then paste in the three commands from the GitHub page.
git config --global credential.helper manager-core
git pull
Final Thoughts
That’s all there is to setting up an existing Rproject with a GitHub repository. Now when you are ready to commit changes, select the Git tab of in the Environment pane. From there:
- Select Commit
- Type in a Commit message
- Select Commit
- Select the Push button
At this point, one can add the README file to describe your repository. It can be added from github.com which would then need to be pulled into the existing branch, or created locally and pushed to github.com.