Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

use_github

Connect a local repo with GitHub


Description

use_github() takes a local project and:

  • Checks that the initial state is good to go:

    • Project is already a Git repo

    • Current branch is the default branch, e.g. master or main

    • No uncommitted changes

    • No pre-existing origin remote

  • Creates an associated repo on GitHub

  • Adds that GitHub repo to your local repo as the origin remote

  • Makes an initial push to GitHub

  • Calls use_github_links(), if the project is an R package

  • Configures origin/DEFAULT to be the upstream branch of the local DEFAULT branch, e.g. master or main

See below for the authentication setup that is necessary for all of this to work.

Usage

use_github(
  organisation = NULL,
  private = FALSE,
  protocol = git_protocol(),
  host = NULL,
  auth_token = deprecated(),
  credentials = deprecated()
)

Arguments

organisation

If supplied, the repo will be created under this organisation, instead of the login associated with the GitHub token discovered for this host. The user's role and the token's scopes must be such that you have permission to create repositories in this organisation.

private

If TRUE, creates a private repository.

protocol

One of "https" or "ssh"

host

GitHub host to target, passed to the .api_url argument of gh::gh(). If unspecified, gh defaults to "https://api.github.com", although gh's default can be customised by setting the GITHUB_API_URL environment variable.

For a hypothetical GitHub Enterprise instance, either "https://github.acme.com/api/v3" or "https://github.acme.com" is acceptable.

auth_token, credentials

Deprecated lifecycle: No longer consulted now that usethis uses the gert package for Git operations, instead of git2r; gert relies on the credentials package for auth. The API requests are now authorized with the token associated with the host, as retrieved by gh::gh_token().

Git/GitHub Authentication

Many usethis functions, including those documented here, potentially interact with GitHub in two different ways:

  • Via the GitHub REST API. Examples: create a repo, a fork, or a pull request.

  • As a conventional Git remote. Examples: clone, fetch, or push.

Therefore two types of auth can happen and your credentials must be discoverable. Which credentials do we mean?

  • A GitHub personal access token (PAT) must be discoverable by the gh package, which is used for GitHub operations via the REST API. See gh_token_help() for more about getting and configuring a PAT.

  • If you use the HTTPS protocol for Git remotes, your PAT is also used for Git operations, such as git push. Usethis uses the gert package for this, so the PAT must be discoverable by gert. Generally gert and gh will discover and use the same PAT. This ability to "kill two birds with one stone" is why HTTPS + PAT is our recommended auth strategy for those new to Git and GitHub and PRs.

  • If you use SSH remotes, your SSH keys must also be discoverable, in addition to your PAT. The public key must be added to your GitHub account.

Git/GitHub credential management is covered in a dedicated article: Managing Git(Hub) Credentials

Examples

## Not run: 
pkgpath <- file.path(tempdir(), "testpkg")
create_package(pkgpath)

## now, working inside "testpkg", initialize git repository
use_git()

## create github repository and configure as git remote
use_github()

## End(Not run)

usethis

Automate Package and Project Setup

v2.0.1
MIT + file LICENSE
Authors
Hadley Wickham [aut] (<https://orcid.org/0000-0003-4757-117X>), Jennifer Bryan [aut, cre] (<https://orcid.org/0000-0002-6983-2759>), RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.