Software Carpentry

Southampton RSG

0. Introduction

Before We Start

  • Create an account at github.com
  • Open up a terminal
  • Download the materials
  • git clone https://github.com/Southampton-RSG/swc-git-novice

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
Changes are tracked sequentially

Version Control Tracks Changes

Different versions can be saved
Different versions can be saved

Version Control Tracks Changes

Multiple versions can be merged
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 Kraken Desktop

2. Setting Up Git

Getting Demo Files

  • git clone https://github.com/Southampton-RSG/swc-git-novice

Key Commands

  • git config

3. Creating a Repository

Key Commands

  • git init
  • git status

4. Tracking Changes

Key Commands

  • git add
  • git commit

Adding & Committing

Repository structure
Repository structure

History

  • git log
  • git diff

Differences

Types of differencing
Types of differencing

5. Exploring History

More Differences

Differences of specific commits
Differences of specific commits

Restoring Files

  • git checkout

Restoring Files

Restore files to specific commits
Restore files to specific commits

6. Collaborating

Local Repo

Local repository workflows

Collaboration

Collaboration via remote repository
Collaboration via remote repository

Remote Repositories

  • Sign in to github.com
  • Create repository
  • git remote add
  • git push

Branches

Branching off a master 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

Branches

Remote workflows
Remote workflows

Branches

Feature-branch workflow
Feature-branch workflow

Exercises

  • Create a new branch called doc coming off dev
  • Add a README.md file
  • Commit your change to doc, then push
  • Create a pull request on GitHub
  • Merge doc into dev, and pull dev back

7. Conflicts

Changes

One set of changes
One set of changes

Conflicting changes

Two conflicting changes
Two conflicting changes

Branch 1

  • git checkout dev
  • git checkout -b feature_cm
  • Add a line to the end of rainfall_conversion.py and commit
  • git push -u origin feature_cm

Branch 2

  • git checkout dev
  • git checkout -b feature_m
  • Add a line to the end of rainfall_conversion.py and commit
  • git push -u origin feature_m

Pull requests

  • Create a pull request from feature_cm to dev
  • Create a pull request from feature_m to dev

Conflict

  • git pull origin dev
  • Fix the conflict
  • Commit and push
  • Okay your pull request

Remote workflows

Two conflicting changes
Two conflicting changes

8. Ignoring Things

Key Files

  • .gitignore
  • .gitkeep