Difficulty using usethis::use_github()

I am trying to use usethis::use_github() to create a repository for a local project.

Following the documentation for the function, I first setup git using usethis::use_git(). This worked fine.

Then, I used usethis::browse_github_pat() to obtain a personal access token (PAT). I then used usethis::edit_r_environ() and added this line: GITHUB_PAT=[40-character PAT].

Finally, I used usethis::use_github(), and got the following message and then error:

Selection: 1
βœ” Creating GitHub repository
βœ” Adding GitHub remote
βœ” Pushing to GitHub and setting remote tracking branch
Error in .local(object, ...) : 
  Error in 'git2r_push': error authenticating:

I tried looking into the error (i.e., this issue in the git2r repo) but had trouble gaining traction. Any tips on how to get this up and running?

1 Like

Are you on Windows? Are you using ssh? Is you ssh key protected by a passphrase? That particular combination has been difficult in the past and there also seems to be a specific problem with git2r at the moment. See, for example, https://github.com/ropensci/git2r/issues/327 (I suspect that may get re-opened).

While the git2r issue plays out, you might want to test drive with HTTPS.

Some further resource on this:

Why git2r <--> remote via ssh can be broken, even though other methods of using git are not:

How to work with git via HTTPS protocol (this is what GitHub actually recommends, btw):

5 Likes

Sorry to come back to this this late. I have dove into the book (thanks for sharing that - I've started reading part before and found that it was immensely helpful). To answer a few preliminary Q's: no, I'm not using Windows (I'm using macOS 10.13.13); I think I'm using https, as when (either in a repository that is already setup or one setup using usethis::use_git() and usethis::use_github() I type git remote -v in Terminal, a remote repository with an URL that begins with https is returned. However, when I followed the steps to find out whether I have HTTPS or SSH setup, it looks like I also have SSH key setup. I think I set this up a long time ago :frowning:

One detail I noticed is that when using usethis::use_git() and usethis::use_github(), a remote repository is created, i.e., in R Studio, I:

  • created a new, empty project, titled "test"
  • used usethis::use_git()
  • used usethis::use_github()

A repository named "test" is created on GitHub (here's the link to it). Is this possibly working in spite of the error?

@jmichaelrosenberg There are multiple tasks being performed by usethis::use_github(). One task is creating the new repository on GitHub. This step relies on the gh package and your personal access token. The step succeeded as evidenced by the empty GitHub repo you now have.

The step that failed was attempting to push your local Git changes to GitHub (which is why the GitHub repo is still empty). That step relies on git2r and the system libraries it uses to push and pull from remote repositories like GitHub.

As @jennybryan mentioned, this step can fail for all kinds of reasons. Since you only need to do this step once to get your repository setup, I'd recommend opening a Terminal (in RStudio: Tools -> Terminal -> New Terminal), and typing git push origin master. This will allow you to continue working on your project.

3 Likes

It would be good to sort this out for you. Can you record your exact steps and code and error messages, from start to finish, with a new test repo? The above doesn't form a clear picture for me re: your overall use of ssh vs https, as opposed to what you requested in your use_github() call (which defaults to ssh). It's all a bit muddy.

I already know that use_github() is not very resilient to its own partial success, i.e. it can't really pick up where it left off right now, if there's a problem with user input or config: https://github.com/r-lib/usethis/issues/221

But with correct input and config, we should be able to get this working for you.

1 Like

I'm on a Mac (High Sierra 10.13.3) and can pretty much reproduce this exactly. I'd like to see what happens if @jmichaelrosenberg updates to the dev version of git2r, but keeps the CRAN version of usethis. That actually fixed it for me and I was able to push, but with the warning:

Warning message:
'head.git_repository' is deprecated.
Use 'repository_head' instead.
See help("Deprecated") 

I think there may have been two errors when using the CRAN version of each package.

  • One error had to do with usethis::use_github() and supposedly the underlying git2r line: git2r::push(r, "origin", "refs/heads/master", credentials = credentials). When credentials = NULL, this can't seem to look up my (password protected) id_rsa even though I have followed the Happy Git and the GitHub instructions on setting them up (I definitely did the ~/.ssh/config step too). I think it has something to do with the interaction with the underlying ssh-agent.

  • On seeing that error, I tried to bypass with usethis::use_github(credentials = git2r::cred_ssh_key()) which prompts for a password. On debugging, this gets me by the problematic line above and actually successfully pushes, but then fails on utils::head(r). I'm pretty sure this might be a misalignment in the fact that the CRAN version of usethis calls utils::head(), but the CRAN version of git2r still uses the S4 method for head (the dev version of git2r changed this over to S3 and deprecated it in favor of repository_head()).

On installing the dev version of git2r, this seems to have all gone away even when passing credentials = NULL. I do still get the above warning though, so you might want to use repository_head(r) instead of utils::head(r) in usethis.

1 Like

git2r is going to be a tricky moving target for a little while, due to an internal switch from S4 to S3. I'm not sure if/how client packages can write code that works for both versions. It looks like we might have to condition on git2r version. head() is one of the affected calls.

This is happening at the same time as https://githubengineering.com/crypto-deprecation-notice/, which may also be affecting ssh workflows and setup for some.

To be continued. This implies it's very important to report version of git2r and usethis for any success/failure.

2 Likes

Re: this specifically. At the time of the most recent usethis was submitted to CRAN, this code worked for both CRAN git2r and dev git2r. Then, subsequently, git2r added repository_head(), a function which does not exist in any prior/released version of git2r. It's unclear right now how downstream dependencies should adjust to all the changes in git2r. I do watch the repo FWIW.

4 Likes

Sorry for the ridiculous delay.. Thank you for all of your help. I just tried usethis::use_github() again (without having changing anything) and it (expectedly) didn't work. I then installed the development version of git2r, which prompted me to use homebrew (or another method) to install libgit2. That gave a lot of weird messages and didn't seem to work, but when I tried it again, it worked.

1 Like