Version Control with Git

0. Introduction

Before We Start

  • Create an account at github.com
  • Open up a terminal

1. What is Version Control?

What Does It Do?

  • Tracks changes to files
  • Any file can be tracked
  • Text (.txt, .csv, .py, .c, .R, etc.) works best
    • These allow smart diff / merge etc.

Why Use Version Control?

  • A more efficient backup
  • Reproducibility
Revision management

Why Use Version Control?

  • Teamwork

Version Control Tracks Changes

Changes are tracked sequentially

Version Control Tracks Changes

Different versions can be saved

Version Control Tracks Changes

Multiple versions can be merged

Version Control Alternatives

  • Git
    • Distributed
  • Subversion (svn)
    • Centralised
  • Mercurial (hg)
    • Distributed
  • Git most widely used in academia
    • GitHub
    • GitLab

Graphical Version Control

Sourcetree Git Kraken Git Desktop
Sourcetree Git Kraken Git Desktop

GUIs

PyCharm RStudio VS Code
PyCharm RStudio VS Code

2. Setting Up Git

Key Commands

  • git config

Setting Up GitHub

3. Creating a Repository

GitHub Template

Downloading a Repository

  • git clone git@github.com:yourname/climate-analysis

4. Tracking Changes

Key Commands

  • git add
  • git commit

Adding & Committing

Repository structure

History

  • git log
  • git diff

Differences

Types of differencing

5. Exploring History

More Differences

Differences of specific commits

Restoring Files

  • git checkout

Restoring Files

Restore files to specific commits

6. Remote Repositories

Local Repo

Local repository workflows

Remote Backups

Mountbatten Fire

Collaboration

Collaboration via remote repository

Remote Repositories

  • git push
  • git pull

Remote Commands

Remote workflows

7. Branches

Feature-branch

Branching off a master branch

Creating branches

  • git branch dev
  • git checkout dev

Branch files

  • Create rainfall_conversion.py
  • git add rainfall_conversion.py
  • git commit -m

Pushing & merging

  • git push origin dev
  • git switch main
  • git merge dev

8. Ignoring Things

Key Files

  • .gitignore
  • .gitkeep