Adding R Projects and Scripts to GitHub

Personal reference — setting up a new project repo with only selected scripts, and adding scripts to an existing repo later

Assumes: GitHub token already stored (done once via gitcreds::gitcreds_set()), and the usethis package installed. All code runs in the RStudio Console with the project open.

Placeholders look like <this> — replace with your actual file or folder names, no angle brackets.


First: The Git Tab (and What If It’s Missing)

The Git tab (top-right pane, next to Environment/History) is per-project, not a permanent part of RStudio. It only appears when the currently open project is a Git repository. So:

  • New project, never set up for Git: no Git tab is normal and expected. You haven’t missed anything — the tab is created by the setup in Part 1 (specifically the use_git() step plus the restart). Don’t go looking for it before then.
  • Project you’ve already set up: the Git tab should be there every time you open it. If it isn’t, work through the checks below.

If the Git tab is missing from a project that should have one

  1. Confirm you’re actually in the project. Look at the very top-right corner of RStudio — it should show the project name. If it says “Project: (None)”, you opened the script file directly instead of the project. Close it and enter through the .Rproj file. This is the most common cause.

  2. Confirm the folder is really a repo. In the Console, run:

    cat(system("git status", intern = TRUE), sep = "\n")

    If this prints branch info, the repo is fine and the problem is only RStudio’s display — go to step 3. If it says “fatal: not a git repository”, this project was never set up (or you’re in the wrong folder) — run Part 1 from the top.

  3. Restart RStudio (Session → Restart R sometimes isn’t enough — fully close and reopen via the .Rproj file). The Git tab is only drawn when the project loads, so changes made mid-session often need a full reopen to show up.

  4. Check RStudio can find Git at all. Tools → Global Options → Git/SVN. The “Git executable” box should show a path like C:/Program Files/Git/bin/git.exe — not “(Not Found)”. If it’s empty or wrong, click Browse and point it to git.exe, then OK and restart RStudio. To confirm Git is installed at all, run:

    system("git --version")

    If that errors, Git itself isn’t installed on the machine — install it from git-scm.com (defaults are fine), restart RStudio, and set the path as above. This matters on a new or reimaged work computer; on your current machine Git is already installed and configured.

Remember: no Git tab doesn’t mean Git is broken or your work is lost. The repo lives in the project folder on disk; the tab is just RStudio’s window onto it. The Console commands work either way.


Part 1: Adding a New R Project to GitHub (Selected Scripts Only)

Use this when a project has never been on Git. The order matters: the ignore rules go in first, so data files are fenced off before the first commit ever happens.

Step 1 — Open the project

Open the project in RStudio by double-clicking its .Rproj file. Confirm the project name shows in the top-right corner of RStudio.

Step 2 — Turn the project into a Git repository

In the Console, run:

usethis::use_git()

It may ask “Is it ok to commit them?” about existing files — answer with the negative option (“Negative” / “No way” / “Absolutely not” — the wording is randomized). You want to choose files yourself, not commit everything. It will then ask to restart RStudio — say yes. After the restart, the Git tab appears.

If the restart prompt never appears, or the restart fails: the prompt is a convenience, not a requirement. Close RStudio completely (fully exit, not Session → Restart R) and reopen the project via the .Rproj file — the Git tab is drawn at project load. Note that rerunning use_git() on a repo that already exists will not offer a restart again (there’s nothing left for it to create); no restart prompt on a rerun usually means the first run already worked. Confirm with git status as in the Git tab section above.

Step 3 — Fence off the data files

In the Console, run (edit the list to match the project):

usethis::use_git_ignore(c("*.csv", "*.xlsx", "*.zip",
                          "<folder_to_exclude>/", "<another_file.txt>"))

Anything matching these patterns disappears from the Git pane permanently — the files stay on your computer, Git just stops offering them. This is the guardrail that prevents client data from ever being staged by accident. When in doubt, ignore more; you can always un-ignore later.

Step 4 — Commit only the scripts you want

  1. Go to the Git tab (top right). Check the box next to each script you want on GitHub — e.g., <script_one>.R and <script_two>.R. Also check .gitignore. Leave everything else unchecked.
  2. Click Commit, type a message (e.g., “Initial commit — adding