These notes were made whilst going through Jenny Bryan’s Happy Git and GitHub for the useR instructions.

Set up Git and GitHub

GitHub

  • Register a GitHub account.

  • Turn on two-factor authentication under: Settings - Account security.

Git

  • Install Git which can be done through Git for Windows.

  • Optionally install a Git client, for example GitHub Desktop, or other recommended clients.

  • In R Studio change terminal to open with Git Bash: Tools - Global Options - Terminal - New terminals open with - Git Bash.

Check Git installed on local machine

Git commands

where git
git --version

Set Git details

usethis::use_git_config set Git user name and email

usethis::use_git_config(user.name = "USERNAME", user.email = "EMAIL")

Check Git user name and email details

Git commands

git config --global --list

Connect RStudio to Git

  • RStudio Tools - Global Options - Git/SVN - Git executable: set path, eg C:/Program Files/Git/bin/git.exe. Ensure Enable version control interface for RStudio projects is ticked.

  • Check for common issues and solutions in Jenny Bryan’s instructions.

Cache credentials for HTTPS

usethis::create_github_token Get Personal access taken (PAT), storing token in a password management system

gitcreds::gitcreds_set Store credentials in Git credential store

gitcreds::gitcreds_set()

Clone own repositories

Clone own remote repository from GitHub

  • Create a repository under Repository.
  • GitHub: Code - Copy HTTPS URL to clipboard.

usethis::create_from_github Clones a remote repository from GitHub, creating a local project and Git repository.

usethis::create_from_github(repo_spec = HTTPS URL,
                            destdir = "DESTINATION DIRECTORY PATH"
                            fork = FALSE)

Git commands

git clone HTTPS URL

Clone own RStudio project to GitHub

usethis::use_git Makes project a Git repository

usethis::use_git()

Git commands

git init

Git commands

git add -A
git commit -m "COMMIT MESSAGE"
git commit --amend --no-edit

usethis::use_github Create a new repository on GitHub

usethis::use_github()
  • Pull files from GitHub repository: RStudio Git - Pull button.

  • Push committed files to GitHub: RStudio Git - Push button.

Git commands

git pull
git push

Work on a branch

  • Create and switch to a new branch: RStudio Git - New Branch button

Git commands

git branch BRANCH_NAME
git checkout BRANCH_NAME
  • Switch and merge branch back into master.

Git commands

git checkout master
git merge BRANCH_NAME
  • Resolve merge conflicts, edit file either keeping contents of branch or master or modified version of both. Stage edited file and commit to master.

    <<<<<<< branch contents =======
    ======= master contents >>>>>>>

Git commands

git status
git add EDITED_FILE_NAME
git commit -m "COMMIT MESSAGE"

-- If need to abort merge
git merge --abort

Revert back to previous state

  • changes saved but not committed RStudio Git - Diff - Discard All / Discard chunk

Git commands

git reset --hard HEAD
  • amend previous commit message

Git commands

git commit --amend -m "New commit message"
  • changes committed (revert to previous commit)

Git commands

-- revert to previous commit, saved and staged changes kept
git reset --soft HEAD^

-- revert to previous commut, saved changes kept but now unstaged
git reset HEAD^

-- revert to previous commit, saved and staged changes lost
git reset --hard HEAD^

Clone others repositories

Clone others remote repository from GitHub

In others remote repository on GitHub

  • GitHub: Fork - Copy others remote repository to your GitHub account.

In your repositories on GitHub

  • GitHub: Code - Copy forked repository in your GitHub account to HTTPS URL to clipboard.
  • RStudio: New Project - Version Control - Git - Repository URL - paste HTTPS URL

Git commands

git clone HTTPS URL

In others remote repository on GitHub (so that able to pull changes from other remote repository)

  • GitHub: Copy others remote repository to HTTPS URL to clipboard.
  • RStudio: Add remote: RStudio Git - New Branch button - Add remote - Remote Name - Paste OTHERS REMOTE HTTPS URL.

Git commands

git remote add upstream OTHERS REMOTE HTTPS URL

usethis::create_from_github Clones and forks others remote repository from GitHub, creating a copy of the repository on your account in Github, a local project and a Git repository, and adds the others remote repository as an upstream remote repository to allow pulling file changes from this upstream remote.

usethis::create_from_github(repo_spec = "HTTPS URL",
                            destdir = "DESTINATION DIRECTORY PATH",
                            fork = TRUE)
  • Check current remotes contains both origin (forked copy of others repository in your github account) and upstream (copy of others repository) repositories.

Git commands

git remote -v
  • Pull changes from others remote repository into master branch, then optionally push to your forked copy (Push committed files to GitHub: RStudio Git - Push button).

Git commands

git pull upstream master --ff-only
git push
  • Create and switch to a new branch: RStudio Git - New Branch button. Make sure Synch branch with remote is ticked

Git commands

git branch BRANCH_NAME
git checkout BRANCH_NAME
  • Only work on this branch, committing and pushing changes to the forked repository in your GitHub account

  • GitHub: Compare and pull request button - Create pull request to submit pull request to others remote repository