11 Git Setup

To use Git, you must first give it some information. This needs to be done once on each computer you use and once in Posit Cloud if you are using that. If you are using a computer in a campus computer lab, all your documents and git settings are deleted each day, so you will need to do this step each time you sign in.

11.1 Introduce yourself to Git

Whether you use Posit Cloud or install the software yourself, you will need to introduce yourself to Git before you can start using it. Quick instructions are provided below. For more detailed instructions, see Introduce yourself to Git (happygitwithr.com) and the Git documentation 1.6 Getting Started - First-Time Git Setup (git-scm.com).

What does “introduce yourself” mean? Basically it just means you need to give Git a name and email address it can attach to all the commits (changes) you make to your repositories.

There are two ways to introduce yourself: 1) by using the shell, or 2) via code in an R script. The shell works best if you need to do it once, for example if you are using Posit Cloud or have installed the software on your own computer. An R script works best if you are using a campus computer lab where your account is deleted at the end of every day.

11.1.1 Option 1: Use the shell

One way to introduce yourself is to use the command line in a shell. A shell is a simple program on your computer that lets you run commands to do things. Every Windows, macOS, or Linux computer has one, even if you’ve never seen it. You may also the shell called a “terminal” or “command line”.

STEP 1: Open a Git command line in one of the following three ways (the first is the easiest):

  • In RStudio running on any computer or in Posit Cloud, do one of the following:

    • Click the Terminal tab in RStudio (bottom left pane next to the Console tab) or use the keyboard shortcut Alt+Shift+M
    • Click the Git tab in RStudio (top right pane), click the More icon (a blue gear), and click Shell… which will open a shell window
  • In Windows:

    • run the application Git CMD, for example by clicking on the icon in the Windows Start Menu
  • In macOS:

    • open a command line

Step 2: Run the following two commands.

$ git config --global user.name "Jane Doe"
$ git config --global user.email janedoe@example.com

replacing the example name and email address with your own. The username can be whatever you want, but I recommend your first and last name. The email address should be the one associated with your GitHub account.

You should enter one command, press enter, then enter the second command, and press enter.

Note, do not enter the $ sign at the beginning, that is just how we note a command prompt.

Note, the username needs double quotes around it because it contains a space. The email does not, although it doesn’t hurt anything to use quotes there as well.

You can check to make sure the commands work by printing the global configuration settings with this command:

git config --global --list

You should see your user name and user email listed there.

11.1.2 Option 2: Use R code

This option works well if you are using a campus computer lab where your account is deleted at the end of every day. Thanks to Happy Git and GitHub for the useR for the inspiration for this.

This method requires the usethis package, so you will first need to install the package. Then run:

# install if needed:
# install.packages("usethis")

library(usethis)
use_git_config(user.name = "Kari Nordmann", user.email = "kari@example.org")

You can install the package by highlighting that line without the comment character and running it.

Note: this only needs to be run once until your user account is deleted from the computer, but it doesn’t hurt to run it again.