Introduction to Git

plus GitHub, R & RStudio

OJ Watson

2025-01-21

Introduction to Git

  • Git is a version control software (VCS).
  • VCS: Takes a picture of what your files look like and stores that in memory.
  • This allows us to look back and see how our files have changed over time.

Why do we need version control?

Version control: metadata + history

  • who: name of person making changes
  • when: date and time change
  • what: contents of files changed
  • why: motivation for the change

Is it just like a backup?

  • Why not just use Dropbox?

  • Or Google Drive?

  • These are also great to use, and Google docs for example has version control where changes are tracked.

  • However, Git and Github are made specifically for tracking how codebases have changed.

Installing Git

Git supports all operating systems. You can install it using command-line tools or directly download and install the setup.

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

  • Linux

    • For Debian/Ubuntu-based operating systems use apt-get install git or similar.
  • macOS

    • If you have homebrew installed, use this command to download and install Git: brew install git.
  • Windows

    • Installing Git on Windows is hassle-free. Just go to the download page

How to check if you have installed Git?

Open a terminal, type git --version and press enter

Basic git terms

  • repository: (or “repo”) git’s database. Might be local or on GitHub
  • working copy: all the files you actually work with
  • commit:
    • as a noun: a snapshot of all the files in the repository
    • as a verb: create a snapshot
  • hash: fingerprint of a file/repository

Git history

Interacting with Git

  • From the command line - always good to be comfortable with this
  • From a GUI
    • RStudio has one built-in
    • Most IDEs do now (e.g. VSCode or Positron)
  • Via GitHub

Basic git commands

  • git init: initialise a repository
  • git add: stage files
  • git commit: add staged files to the repository by making a commit
  • git log: see an overview of what changed
  • git diff: see details of what changed

End of part 1

What makes a commit?

Ideally

  • One set of related changes
  • Commit message explains why they were needed
  • Short title (<60 characters)
  • Blank line, then any further discussion

Not everything goes in git

  • Large files
  • Passwords
  • Outputs (sometimes)
  • Use .gitignore

Remote git commands

  • git clone: download a repository
  • git fetch: update remote changes but don’t apply them
  • git pull: update remote changes and do apply them
  • git push: send your changes upstream
  • git remote: inspect / change details of upstream repositories
  • git branch: inspect / create a branch
  • git checkout -b: create a branch and change to it (yes this is stupid)
  • git merge: merge branches

Rstudio and GitHub

  • Not all commands are supported but all you will likely need
  • Use the shell for the rest