Words to the wise

Vocabulary

Initial setup when using git and github.com for the first time with a given computer


Step 1: Tell your computer who you are

These commands should be typed into Terminal. This tells git about the identity of the “person working with git on this computer.”

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

For example, I would type:

git config --global user.email "spielman@rowan.edu"
git config --global user.name "Stephanie Spielman"


Step 2: Tell github.com who your computer is

These commands should be typed in R Console, making use of the handy credentials R package. You can also accomplish this with different commands in the Terminal, but this is much less irritating, I promise.

First, enter the following into the R Console in RStudio Cloud. We’ll need its output:

# Generate an SSH Key to pair with GitHub
credentials::ssh_keygen()

Then…

  1. Navigate to github.com, and go to Settings –> “SSH and GPG Keys” (or click https://github.com/settings/keys).
  2. Click the big green button in the top right: “New SSH Key”
  3. Title your key “RStudio Cloud”
  4. In the “Key” field, paste the full output from $pubkey, excluding the quotes.
  5. Click the big green button “Add SSH Key.” All set!!



The very basic git command reference

Command Description
git clone <ssh repo address> Clone the repository to a new computer. This should only be run ONCE. This like like downloading the repository.
git add filename Stage a file with changes. Use this to tell git: There are changes here you should know about.
git commit -m "message" Commit files that have been added/removed with a message! Don’t forget to type the -m "message" - you will be stuck in VIM if you forget, and that will not be pleasant.
git push Send commit(s) to github.com
git pull Obtain commits pushed to github.com onto the current machine. When working on multiple computers or with contributors, you should do this EVERY TIME you start working again in the project.
git mv old_filename new_filename Move a file and track this move with git in a shortcut version of mv old_filename new_filename; git add new_filename (Note: there is no analogous shortcut for cp). Use this to mv files that git knows about!
git rm filename Remove a file from being tracked by git entirely. Hopefully you won’t have to do this :).
git status Check the status of files.
git checkout filename Obtain the most recent commited version of the file. Use this if you screwed something up and need to “reset” to the last COMMITTED version. It’s like an “Undo” button!
git log See past commits and associated messages (may need to press the q key to exit)

The individual git workflow

At any and all times, running git status will show you what is staged, what is tracked but not staged, and what is not tracked at all.

  1. Obtain a local version (aka, on your computer) of a repository with git clone <repo ssh address>
  2. Use cd to navigate into that repository’s directory in the terminal.
  3. Add files for staging with git add <filename>
    • Any time you make a change to a file, it must be re-added
    • You can also remove files from being tracked entirely by git with git rm <filename>. You probably won’t need this for your project.
    • If the desired change is to RENAME a file that git is already tracking, it is way easier to use: git mv <original> <new>
  4. Stage changes (anything you have added/removed/moved) with git commit -m "An informative message that broadly says what the commit does"
    • If you forget the -m, you’re going to have a very bad time. Like, BAD.
  5. Push changes to the github.com repository versions with git push
    • Depending on your local setup, you may be prompted for your github.com username and password every time.
  6. Update your local repository with the github.com version of the repository with git pull
    • This is ALWAYS STEP 1 when returning to a project! YOU DON’T WANT CONFLICTS



Different types of output from git status


Everything is up to date and there are no new changes.
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean


New files are present, but git doesn’t know about the files yet.
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    new_file.txt

nothing added to commit but untracked files present (use "git add" to track)


New files have been ADDED but not yet COMMITTED.
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   new_file.txt


Files have been committed, but not yet PUSHED to the github website
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean



Miscellaneous Troubleshooting

Some messages you might see, what they mean, and how to deal with them:


#### When running pretty much any git command:

fatal: not a git repository (or any of the parent directories): .git

This means you are running git commands but you are not in a git repository! You have to cd into the a repository’s directory to run git commands.


When trying to add or remove a file:

fatal: pathspec <name of file> did not match any files

This means you are trying to add or remove a file that doesn’t exist! Potentially, the file isn’t there at all - (did you spell it wrong? are you in right directory? However, if the file does exist and you can’t git rm it, that’s because the file isn’t actually under version control! git doesn’t already know about the file so it doesn’t want to remove it. Instead, you need to just remove the file regularly with plain rm instead of git rm.


When trying to commit

error: switch `m' requires a value

You forgot the commit message! You can’t just type git commit -m - you need git commit -m "YOUR MESSAGE GOES HERE".