Chapter 4 Personal Access Token
GitHub requires a Personal Access Token (PAT) instead of passwords for Git operations.
4.1 Preferred Method: R + RStudio
If you don’t already have R and RStudio installed, it’s very easy and highly recommended to install them:
- R: Download from https://cran.r-project.org/
- RStudio: Download from https://posit.co/download/rstudio-desktop/
Both are free and install quickly on all major operating systems.
The easiest way to set up GitHub authentication is to run the following commands in R or RStudio:
install.packages(c("usethis", "gitcreds"))
usethis::create_github_token() # Opens browser to create PAT
gitcreds::gitcreds_set() # Enter your PAT when promptedWhen you run usethis::create_github_token(), it will open your browser to GitHub where you’ll need to:
- Click “Generate new token (classic)”
- Give it a descriptive name (e.g., “Academic Projects 2024”)
- Set expiration (recommended: 90 days for learning, 1 year for regular use)
- Select scopes (the function pre-selects the right ones, but verify you have repo, workflow, and user)
- Click “Generate token”
- Copy the token immediately - you won’t see it again! Store it in a password manager for safekeeping.
Then return to R/RStudio and run gitcreds::gitcreds_set() to enter your PAT when prompted.
That’s it!
4.2 Alternative: Git CLI + Credential Helper
If you do not want to install R and RStudio, you can create your PAT manually on GitHub:
Go to GitHub Settings → Developer settings → Personal access tokens → Tokens (classic), then follow the same steps as described above (click “Generate new token (classic)”, give it a name, set expiration, select scopes, generate and copy the token).
Then configure your credential helper:
macOS:
Windows:
When you clone or push, you’ll be asked for:
- Username: your_github_username
- Password: your_PAT_here
✅ Git will remember the token after this — no need to reenter.
4.3 Testing Your Setup
4.3.2 Test Authentication
Try cloning a repository:
When prompted, enter:
- Username: your_github_username
- Password: your_PAT (not your account password!)
Notes:
- If you used the R/RStudio method above, you might not be asked for credentials at all since they’re already stored securely.
- If using the credential helpers (explained above), Git will remember your token for future use.
4.4 Troubleshooting
4.4.1 Authentication Failed
Common issues:
- Using account password instead of PAT
- Token has expired
- Insufficient token permissions
- Typo in username or token
Solutions:
- Verify you’re using the PAT, not your password
- Check token expiration date
- Ensure token has “repo” scope
- Try clearing credentials and re-entering
Next Chapter Preview: Now that you have Git installed and authentication configured, we’ll learn the essential Git commands for daily workflow: add, commit, push, and pull.