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 --global user.name "Your Name"
  • git config --global user.email "yourname@gmail.com"
  • git config --global core.editor "nano -w"

Setting Up GitHub

3. Creating a Repository

GitHub Template

Key Commands

  • git clone git@github.com:yourname/climate-analysis
  • cd climate-analysis
  • ls -a
  • git status

4. Tracking Changes

Key Commands

  • nano README.md
  • git add README.md
  • git status
  • git commit -m "Your message"

Adding & Committing

Repository structure

Key Commands

  • git status
  • git log
  • nano climate_analysis.py
  • git diff
  • git add climate_analysis.py
  • git commit -m "Your message"

Challenge

  • Use nano to edit climate_analysis.py
  • Add “# TODO: Add rainfall processing code” to the end
  • Commit the change to the repository

Solution

  • nano climate_analysis.py
  • git diff
  • git add climate_analysis.py
  • git commit -m "Your message"

5. Exploring History

Key Commands

  • git log
  • git diff HEAD~1 climate_analysis.py
  • git diff HEAD~2 climate_analysis.py

Challenge

  • Get the ID of your first commit
  • Get a summary of the changes to climate_analysis.py since then

Solution

  • git log
  • Take the first 7 characters of the last commit
  • git diff <COMMIT ID> climate_analysis.py

More Differences

Differences of specific commits

Key Commands

  • rm climate_analysis.py
  • git status
  • git restore climate_analysis.py

Advanced Use

  • git checkout <COMMIT ID> climate_analysis.py

Restoring Files

Restore files to specific commits

6. Remote Repositories

Local Repo

Local repository workflows

Remote Backups

Mountbatten Fire

Collaboration

Collaboration via remote repository

Key Commands

  • git push

Conflict Creation

Local:

  • nano README.md
  • Add your email to the end
  • git commit -am "Your message"

Remote:

  • Go to your repo on GitHub
  • Edit README.md to add install info to the end
  • Commit directly to main

Conflict Resolution

  • git pull
  • git config pull.rebase false if it fails
  • nano README.md and remove the <<</===/>>>
  • git commit -am "Your message"
  • git push

Remote Commands

Remote workflows

7. Branches

Feature-branch

Branching off a master branch

Creating branches

  • git branch
  • git branch dev
  • git switch dev

Branch files

  • nano rainfall_conversion.py
  • git add rainfall_conversion.py
  • git commit -m "Your message"
  • ls
  • git log

Switching Branches

  • git switch main
  • ls
  • git log

Pushing Branches

  • git switch dev
  • git push origin dev
  • git config --global push.autoSetupRemote true

Merging Branches

  • git switch main
  • git merge dev

8. Ignoring Things

Create Temporary Files

  • git switch dev
  • mkdir results
  • touch a.dat b.dat results/a.out results/b.out
  • git status

Create Git Ignore

  • nano .gitignore
  • Lines for *.dat and results/
  • git status
  • git add .gitignore
  • git commit -m "Your message"