# Load necessary libraries for GitHub Personal Access Token (PAT) management
library(credentials) # For PAT access
library(gitcreds)
library(usethis)
GitHub Tutorial for Stat 220
This tutorial guides you through creating and cloning a GitHub repository, emphasizing the integration of Git with Posit and addressing credential management across different operating systems.
Initial Setup in Posit
Establishing Git Credentials / PAT
- Configure Git with Your Information: Replace “Your Name” and “youremail@example.com” with your details.
::use_git_config(user.name = "Your Name", user.email = "youremail@example.com") usethis
- Create GitHub Token: Follow prompts to create and configure a GitHub token for authentication.
::create_github_token() usethis
- Set Git Credentials: For subsequent uses, start here to set credentials.
::gitcreds_set() gitcreds
- Verify Credentials:
::gitcreds_get() gitcreds
Credential Caching on Linux
For Linux users, to avoid frequent re-authentication, configure Git to cache credentials:
--global credential.helper 'cache --timeout=10000000' git config
This sets the cache timeout to approximately 16 weeks, suitable for a quarter.
In this tutorial, you will also practice creating a GitHub repository using the usethis::use_github() function and cloning it back to your local machine using Posit’s menu options.
Tutorial 1: Creating and cloning a Repository starting from Github to Posit
Visit the GitHub website at https://github.com and sign in using your GitHub account. If you don’t have an account yet, you can create one for free.
Once logged in, click on the “+” icon in the top right corner of the webpage, then click on “New repository”.
Enter a name for your new repository in the “Repository name” field. You may also provide an optional description.
Choose the visibility of your repository by selecting either “Public” or “Private”. Public repositories are visible to anyone, while private repositories are only visible to you and any collaborators you invite.
(Optional) Check the box to initialize the repository with a README file.
Click on the “Create repository” button to create your new repository.
This will create a new GitHub repository on your Github account. Follow further to clone the repository to your local folder using Posit.
Go to your GitHub repository webpage and click on the green “Code” button. This will display a dropdown menu with a URL for your repository. Click on the clipboard icon to copy the URL to your clipboard.
Open Posit, and from the “File” menu, select “New Project”.
In the “New Project” dialog, choose “Version Control”.
Select “Git” as the version control system.
In the “Repository URL” field, paste the URL that you copied from your GitHub repository webpage.
Choose a local directory where you want to clone the repository by clicking on the “Browse” button and navigating to the desired folder on your computer.
Click on “Create Project” to clone the GitHub repository to your local computer.
Tutorial 2: Creating a new GitHub repository using usethis
R package (Posit to Github) (Works ONLY on local Posit)
Prerequisites
Install the usethis package if you haven’t already: install.packages(“usethis”)
Make sure you have a GitHub account, and you are logged in.
Configure Git with your name and email address if you haven’t already. Run the following commands in the R console, replacing “Your Name” and “youremail@example.com” with your information:
::use_git_config(user.name = "Your Name", user.email = "youremail@example.com") usethis
Create a new R project in Posit by clicking on “File” > “New Project” > “New Directory” > “New Project.” Give your project a name and choose a location on your computer to save it. Click “Create Project.”
Make a new file or copy and paste a .qmd file that you want to have in your repo and save it to your requirement.
- In the R console, load the usethis package:
library(usethis)
- Initialize a Git repository for your project by running:
::use_git() usethis
- Now, let’s create a new GitHub repository using the usethis::use_github() function. Run the following command:
::use_github() usethis
- Follow the instructions in the R console, and your GitHub repository will be created. Note the repository URL, as you will need it in your further activities