# Sign-in to your GitHub account
# Read: https://usethis.r-lib.org/articles/articles/git-credentials.html
# Read: https://usethis.r-lib.org/articles/articles/usethis-setup.html
::create_github_token() # generates a PAT token you can copy/paste in the next step.
usethis::gitcreds_set() # paste the PAT from the previous step
gitcreds
# Now check that your git credentials are set.
# Check whether you have confgigured your git user.name and user.email.
# If not, use usethis::use_git_config()
::gh_whoami()
gh::gh_token_help()
usethis::use_git_config(user.name = "Jane Doe", user.email = "jane@example.com")
usethis::gh_token_help() usethis
Git, GitHub & RStudio
The usethis
package and the RStudio IDE will help you orchestrate your version control repositories as RStudio projects.
Background
This page gives tips on the procedures of configuring Git and GitHub from withing RStudio. The page favors the brevity of highlighting functions in a procedural order. There are also three videos in the sidebar (ranging from 3 minutes to 120 minutes) . The last video explains in more detail why and how you use version control as part of your reproducible workflow.
Setup
To get started, make a free GitHub.com account. Make sure you have the latest versions of R, RStudio, and usethis. Then, setup your RStudio configuration using two key documents: Setup, and Managing Git(Hub) Credentials.
Note: Check to see if you already have git on your local workstation. You can do this by running system("git --version")
in the RStudio console. Often, windows users must first manually install Git.
Tips
Configure git and manage GitHub credentials (PAT)
The following are steps you can inovke in your RStudio IDE to securely connect your RStudio instance to GitHub. I recommend invoking these functions in the RStudio console. The steps in the code chunk below only need to be inovked once per RStudio instance, per active Personal Access Tokens (PAT.) First, Log in to GitHub, then it is easy to create new GitHub PATs from the RStudio console: usethis::create_github_token()
.
By default Personal Access Tokens (PATs) expire every 30 days. GitHub tokens (PATs) can be managed at the GitHub developer page.
If this is your first time, you will probably need to run usethis::use_git_config()
. Typically the process of setting your git configuration with your GitHub ID and associated email is only done once, i.e. the first time.
By contrast, you will need to rerun usethis::create_github_token()
each time a PAT expires.
Create a new RStudio Project
It’s recommended to use RStudio to create your local git repository. With this method, after selecting a “New RStudio Project (Directions)”, select the “Create a git repository” option. This will ensure your RStudio project is properly configured as a git repository. Then, add a README file and a license, before connecting your local repository to GitHub.
use_readme.md()
use_ccby_license() # Optionally, add a license
# Before next step → stage & commit changes
use_github() # Connect local repo to GitHub
Alternatively, transform an existing folder
On your local workstation, you can transform a local folder into an RStudio project and also make that project a git repository. Then connect the local repository to GitHub.
This will transform the local folder into an RStudio project. Howver, with this approach you must then create local git repository and then connect that repo to GitHub from local.
library(usethis)
use_git() # initialize a git repo
# use_git_ignore() The .gitignore file is created by default running use_git()
use_readme.md()
use_ccby_license() # Optionally, add a license
# Before next step → stage & commit changes
use_github() # Connect local repo to GitHub
Rename default branch
library(usethis)
git_default_branch()
git_default_branch_rename()
git_default_branch_rediscover()
Clone or fork from GitHub
::create_from_github("https://github.com/data-and-visualization/git-tutorial") usethis
Next Level…
Take your reproducibility to the next level. Make your code citable by connecting your GitHub repo and your ORCID (unique author ID) through to the Zenodo archival repository. Now, every time you commit a milestone release, you also mint a DOI.