Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@z3tt
Last active April 20, 2024 17:00
Show Gist options
  • Save z3tt/3dab3535007acf108391649766409421 to your computer and use it in GitHub Desktop.
Save z3tt/3dab3535007acf108391649766409421 to your computer and use it in GitHub Desktop.
Configure GitHub for Rstudio
#### 1. Sign up at GitHub.com ################################################
## If you do not have a GitHub account, sign up here:
## https://github.com/join
# ----------------------------------------------------------------------------
#### 2. Install git ##########################################################
## If you do not have git installed, please do so:
## Windows -> https://git-scm.com/download/win
## Mac -> https://git-scm.com/download/mac
## Linux -> https://git-scm.com/download/linux
## or: $ sudo dnf install git-all
# ----------------------------------------------------------------------------
### 3. Configure git with Rstudio ############################################
## set your user name and email:
usethis::use_git_config(user.name = "YourName", user.email = "your@mail.com")
## create a personal access token for authentication:
usethis::create_github_token()
## in case usethis version < 2.0.0: usethis::browse_github_token() (or even better: update usethis!)
## Note for Linux users:
## credentials::set_github_pat() (in line 34) might store your PAT in a memory cache that
## expires after 15 minutes or when the computer is rebooted. You thus may wish to do
## extend the cache timeout to match the PAT validity period:
usethis::use_git_config(helper="cache --timeout=2600000") #> cache timeout ~30 days
## set personal access token:
credentials::set_github_pat("YourPAT")
## or store it manually in '.Renviron':
usethis::edit_r_environ()
## store your personal access token in the file that opens in your editor with:
## GITHUB_PAT=xxxyyyzzz
## and make sure '.Renviron' ends with a newline
# ----------------------------------------------------------------------------
#### 4. Restart R! ###########################################################
# ----------------------------------------------------------------------------
#### 5. Verify settings ######################################################
usethis::git_sitrep()
## Your username and email should be stated correctly in the output.
## Also, the report shoud cotain something like:
## 'Personal access token: '<found in env var>''
## If you are still having troubles, read the output carefully.
## It might be that the PAT is still not updated in your `.Renviron` file.
## Call `usethis::edit_r_environ()` to update that file manually.
# ----------------------------------------------------------------------------
## THAT'S IT!
@emkahuda
Copy link

Hi, on the last step, the output did not show the PAT for me. Could you please then show how to do commit from RStudio after doing this PAT configuration? I used to do commit from RStudio directly and quiet easy but now since the Github password authentication was removed, I have no idea how to connect RStudio to Github using the PAT. please help

@z3tt
Copy link
Author

z3tt commented Aug 26, 2021

I am sorry @emkahuda but I can't commit my time to fix GitHub <> Rstudio issues and explain version control workflows. Please check other resources or ask in a forum.

@juanchiem
Copy link

can you use it with two github accounts setting for one pc?

@friendly
Copy link

Perhaps this discussion should be moved to stack exchange rather than continuing to impinge on @z3tt, who provided this as a helpful gist but hadn't figured on providing ongoing support.

@shmuhammadd
Copy link

Hi, simple and easy. Thanks

@z3tt
Copy link
Author

z3tt commented Sep 24, 2021

I am happy it helps so many people but agree with @friendly: this was just a snippet to share with no intention to create a discussion around the topic. I am trying to help here and there but please also consolidate other resources if you're stuck!

@friendly
Copy link

Sorry to revive this, but just want to document a new baffling problem I'm having on two machines. I reported this on SO, https://stackoverflow.com/questions/69305874/authenticating-to-github-from-rstudio

I have my GITHUB_PAT set up properly (I think), and usethis::siterep() reports my PAT is discovered, but I'm still prompted for a password, and authentication fails:

> Sys.getenv("GITHUB_PAT")
[1] "ghp_TaA*****************************************"

> usethis::git_sitrep()
Git config (global)
* Name: 'Michael Friendly'
* Email: 'friendly@yorku.ca'
* Vaccinated: TRUE
i Defaulting to 'https' Git protocol
* Default Git protocol: 'https'
GitHub
* Default GitHub host: 'https://github.com'
* Personal access token for 'https://github.com': '<discovered>'
* GitHub user: 'friendly'
* Token scopes: 'gist, repo, user, workflow'
* Email(s): 'friendly@yorku.ca (primary)', 'michael.friendly@gmail.com'
Git repo for current project
* Active usethis project: 'C:/R/Projects/HistDataVis'
* Default branch: 'main'
* Current local branch -> remote tracking branch:
  'main' -> 'origin/main'
GitHub remote configuration
* Type = 'ours'
* Host = 'https://github.com'
* Config supports a pull request = TRUE
* origin = 'friendly/HistDataVis' (can push)
* upstream = <not configured>
* Desc = 'origin' is both the source and primary repo.
  
  Read more about the GitHub remote configurations that usethis supports at:
  'https://happygitwithr.com/common-remote-setups.html'

nchar(Sys.getenv("GITHUB_PAT"))
[1] 40

Also tried credentials_set_github_pat(), but this fails:

> credentials::set_github_pat("ghp_TaA************************************")
If prompted for GitHub credentials, enter your PAT in the password field
Logon failed, use ctrl+c to cancel basic credential prompt.
error: unable to read askpass response from 'rpostback-askpass'
fatal: could not read Password for 'https://PersonalAccessToken@github.com': terminal prompts disabled
Error: Failed to call 'git credential fill'

@friendly
Copy link

In line 31, delete extraneous ` at end of line

@z3tt
Copy link
Author

z3tt commented Sep 30, 2021

Hi together, so now I had to update my PAT as well and all worked perfectly. I added the step noted in the console explicitly which is: restart R after adding your token to the environment.

@qrzhang
Copy link

qrzhang commented Oct 5, 2021

Awesome job! It saves my day 👍

@aleruete
Copy link

Thank you!

@friendly
Copy link

friendly commented Oct 20, 2021

An update on this: I posted this Q on SO: https://stackoverflow.com/questions/69305874/authenticating-to-github-from-rstudio

One answer that worked for me was to issue this command from my local repo:
git remote set-url origin git@github.com:username/repo.git
This worked after I had tried several other things.

@rmgriffin
Copy link

rmgriffin commented Dec 2, 2021

@friendly Same issue here. This seems to have done the trick on a repo-by-repo basis. Thanks! I'm considering just pasting my PAT in my code at this point - this has been an epic saga to get this to work.

@rmgriffin
Copy link

This seems to be a workaround that is simple: https://stackoverflow.com/a/68199261

@IainWilson-
Copy link

simply awesome.... thanks for this

@simazhi
Copy link

simazhi commented Jan 5, 2022

Thank you so much!

@tiagodsferreira
Copy link

this was very useful! Thanks!

@steverugi
Copy link

many many thanks for your help, all works fine

@tomhanna-uh
Copy link

This is great. Thank you! One minor suggestion. I wasn't familiar with the usethis::edit_r_environ() and kept trying to figure out how to enter the PAT in the parentheses. A comment like this might help people as clueless as me: #Run this line as is to start the editor

@PeterTOC
Copy link

This was godsent
Thank you!

@z3tt
Copy link
Author

z3tt commented May 29, 2022

Thanks for the feedback @tomhanna-uh, I adjusted the comments on the usethis::edit_r_environ() bit, hope it is easier to understand now.

@swvanderlaan
Copy link

Awesome!

@yh3430
Copy link

yh3430 commented Jan 17, 2023

Thank you. This is an easy instruction!

@robertwwalker
Copy link

You saved me having to write a poorer version for students this term. Can't thank you enough, Cedric!

@z3tt
Copy link
Author

z3tt commented Jan 26, 2023

Glad so many still find it helpful. And all the best with your course Robert!

@elkeblange
Copy link

Thanks a lot! Very happy to have found your advice on this, after 2 days trial-and-error... Yes, still very helpful for me as newbie :)

@bentonelli
Copy link

Very helpful, thanks!!

@anastasia-radeva
Copy link

Glad I found this relatively early. These kinds of setups take hours sometimes. Thank you!

@grady
Copy link

grady commented Oct 13, 2023

Under default settings on Linux, credentials::set_github_pat might store your PAT in a memory cache that expires after 15 minutes (or when the computer is rebooted), potentially leading to a great deal of frustration shortly later.

Linux users may wish to do either extend the cache timeout to match the PAT validity period

usethis::use_git_config(helper="cache --timeout=2600000") #cache timeout ~30 days

or configure one of the alternative methods that stores the PAT to disk (store or libsecret based solutions).

@z3tt
Copy link
Author

z3tt commented Oct 16, 2023

Thank you @grady , I#ve added a note to the gist!

@grady
Copy link

grady commented Oct 19, 2023

@z3tt To be effective, the config change to the cache settings needs to occur before setting the PAT. (currently line 28)

@z3tt
Copy link
Author

z3tt commented Oct 19, 2023

Thanks @grady, I have updated the snippet aaccordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment