Our git backgrounds

Amelia

  • Post-doc at the University of Edinburgh using epidemiological and statistical methods for mental health research.

%%{init: {
  'theme': 'base',
  'gitGraph': {
    'showBranches': true,
    'showCommitLabel':false
  },
  'themeVariables': {
    'tagLabelFontSize': '18px'
  }
}}%%


gitGraph
   commit tag: "PhD/post-doc"

   branch data-analysis
   commit tag: "R"
   commit tag: "Python"

   checkout main
   branch reproducible-pipelines
   commit tag:"nextflow"

   checkout main
   branch github-actions
   commit tag:"testing"
   commit
   commit tag:"GitHub pages"

   checkout main
   merge data-analysis
   merge reproducible-pipelines
   merge github-actions

Joe


  • Pathogen transmission using genomics
  • Continued at University College Dublin
  • Data science in international development
  • Data science in University Services



Has anyone used version control tools like git and GitHub?

Version control

*Image taken from Intro to Reproducible Research in R

What is git?

“You use git to take snapshots of all the files in a folder.”

Alice Bartlett

Snapshots in time


Snapshots in time


Snapshots in time


A record of:

  • What changes were made
  • Who made them
  • When they made them

Key concepts


  • Repository - your project folder
  • Commit - snapshot of your folder
  • Branch - working version of your folder
  • Collaboration - working together on codebase with GitHub/GitLab/Azure DevOps/…

Key concepts


  • Repository - your project folder
  • Commit - snapshot of your folder
    • We push snapshots online
    • And pull other’s snapshots to local
  • Branch - working version of your folder
  • Collaboration - working together on codebase

Difference between git and GitHub

  • git is a version control system tracking changes to your code locally.
  • GitHub is a cloud-based platform for hosting git repositories and working collaboratively.



Has anyone worked collaboratively on a codebase?

git branch = a new/separate version of the main respository

git branch = a new/separate version of the main respository



%%{init: {
  'theme':'base',
  'gitGraph': {
    'showBranches': true,
    'showCommitLabel':false
  },
  'themeVariables': {
    'tagLabelFontSize': '18px'
  }
}}%%

gitGraph
   commit
   commit
   commit
   commit
   commit
   commit
   commit
   commit
   commit
   commit
   commit
   commit
   commit

git branch = a new/separate version of the main respository


git branch initial_content
git checkout initial_content

%%{init: {
  'theme':'base',
  'gitGraph': {
    'showBranches': true,
    'showCommitLabel':false
  },
  'themeVariables': {
    'tagLabelFontSize': '18px'
  }
}}%%

gitGraph
   commit
   commit

   branch initial_content
   commit

git branch = a new/separate version of the main respository


git add <file_name>
git commit -m "<message>"

%%{init: {
  'theme':'base',
  'gitGraph': {
    'showBranches': true,
    'showCommitLabel':false
  },
  'themeVariables': {
    'tagLabelFontSize': '18px'
  }
}}%%

gitGraph
   commit
   commit

   branch initial_content
   commit
   commit
   commit
   commit

git branch = a new/separate version of the main respository


git checkout main
git merge initial_content

%%{init: {
  'theme':'base',
  'gitGraph': {
    'showBranches': true,
    'showCommitLabel':false
  },
  'themeVariables': {
    'tagLabelFontSize': '18px'
  }
}}%%

gitGraph
   commit
   commit

   branch initial_content
   commit
   commit
   commit
   commit

   checkout main
   merge initial_content

git branch = a new/separate version of the main respository



%%{init: {
  'theme':'base',
  'gitGraph': {
    'showBranches': true,
    'showCommitLabel':false
  },
  'themeVariables': {
    'tagLabelFontSize': '18px'
  }
}}%%

gitGraph
   commit
   commit

   branch initial_content
   commit
   commit
   commit
   commit

   checkout main
   merge initial_content

   branch 3-git-glossary
   checkout 3-git-glossary
   commit
   commit
   commit

git branch = a new/separate version of the main respository



%%{init: {
  'theme':'base',
  'gitGraph': {
    'showBranches': true,
    'showCommitLabel':false
  },
  'themeVariables': {
    'tagLabelFontSize': '18px'
  }
}}%%

gitGraph
   commit
   commit

   branch initial_content
   commit
   commit
   commit
   commit

   checkout main
   merge initial_content

   branch 3-git-glossary
   checkout 3-git-glossary
   commit
   commit
   commit

   branch 2-git-flow-diagram
   checkout 2-git-flow-diagram
   commit
   commit

git branch = a new/separate version of the main respository



%%{init: {
  'theme':'base',
  'gitGraph': {
    'showBranches': true,
    'showCommitLabel':false
  },
  'themeVariables': {
    'tagLabelFontSize': '18px'
  }
}}%%

gitGraph
   commit
   commit

   branch initial_content
   commit
   commit
   commit
   commit

   checkout main
   merge initial_content

   branch 3-git-glossary
   checkout 3-git-glossary
   commit
   commit
   commit

   branch 2-git-flow-diagram
   checkout 2-git-flow-diagram
   commit
   commit

   checkout 3-git-glossary
   merge 2-git-flow-diagram

git branch = a new/separate version of the main respository



%%{init: {
  'theme':'base',
  'gitGraph': {
    'showBranches': true,
    'showCommitLabel':false
  },
  'themeVariables': {
    'tagLabelFontSize': '18px'
  }
}}%%

gitGraph
   commit
   commit

   branch initial_content
   commit
   commit
   commit
   commit

   checkout main
   merge initial_content

   branch 3-git-glossary
   checkout 3-git-glossary
   commit
   commit
   commit

   branch 2-git-flow-diagram
   checkout 2-git-flow-diagram
   commit
   commit

   checkout 3-git-glossary
   merge 2-git-flow-diagram

   checkout main
   merge 3-git-glossary

git branch = a new/separate version of the main respository


git commands

sequenceDiagram
  box Local
  participant workspace
  participant index
  participant localRepo as Local Repo
  end
  box Remote
  participant remoteRepo as Remote Repo
  end

  remoteRepo->>workspace: git pull
  workspace->>index: git add
  index->>localRepo: git commit
  localRepo->>remoteRepo: git push

  %%{
    init:{
        'theme':'neutral',
        'themeCSS':'.messageLine0:nth-of-type(1) {stroke: #ff6614; stroke-width: 5px; }.messageLine0:nth-of-type(2) { stroke: #ffab24; stroke-width: 5px; }.messageLine0:nth-of-type(3) { stroke: #22a7d4; stroke-width: 5px; }.messageLine0:nth-of-type(4) { stroke: #23ad94; stroke-width: 5px; }.messageLine0:nth-of-type(5) { stroke: #8aa825; stroke-width: 5px; }.messageLine0:nth-of-type(6) { stroke: brown; stroke-width: 5px; }.messageLine0:nth-of-type(7) { stroke: #8c3ab5; stroke-width: 5px; }.messageLine0:nth-of-type(8) { stroke: #999999; stroke-width: 2px; }.messageText:nth-of-type(1) { fill: black; font-size: 20px !important; font-weight: bold; }.messageText:nth-of-type(2) { fill: black; font-size: 20px !important; font-weight: bold; }.messageText:nth-of-type(3) { fill: black; font-size: 20px !important; font-weight: bold; }.messageText:nth-of-type(4) { fill: black; font-size: 20px !important; font-weight: bold; }.messageText:nth-of-type(5) { fill: black; font-size: 20px !important; font-weight: bold; }.messageText:nth-of-type(6) { fill: black; font-size: 20px !important; font-weight: bold; }.messageText:nth-of-type(7) { fill: black; font-size: 20px !important; font-weight: bold; } #arrowhead path {stroke-width: 4px;}.actor rect { font-size: 40px !important; } text.actor {fill: black; font-size: 25px !important; font-weight: bold;}'
        }
    }%%
  %%{
    init: { 'sequence': {'mirrorActors':true} }
  }%%

%% annoyingly I can't figure out a way to increase text size of the words: workspace, index, local repo and remote repo

git commit --message

git commit -m

  • What is a commit message?
    • Text describing changes made in that commit
  • What should you write in a commit message?
    • What change was made.
    • Why the change was necessary.
    • Keep it clear, concise, and meaningful.

git commit -m


git commit -m "add slides on writing git commit messages"

git commit -m


git commit -m "add slides on writing git commit messages" -m "Added slides explaining what a commit message is and how to write one effectively. This helps ensure clear and meaningful commit history for better collaboration."

git commit -m

xkcd.com

git command glossary

Command Definition
git status Check current status of local repository - is it up to date?
git pull Pull changes from GitHub to local repository
git add [FileName] Stage file for committing
git commit -m [message] Commit staged changes to GitHub. Always add meaningful message.
git push Push committed changes to GitHub
git fetch Get latest version references for repository
git stash Stash changes so that I can send to different branch ( I use when I forgot to switch branches before making change. Run this command - then switch to branch - then run command below)
git stash pop Unstash changes that were previously stashed (Used in combination with command above to move changes onto different branch)
git branch List existing branches
git branch [branchName] Create new branch
git push --set-upstream origin [branchName] Push a branch online
git branch -d [branchName] Delete branch
git checkout [branchName] Switch to branch
git checkout -t origin/[branchName] Switch to remote branch (note you’ll need to run git fetch first)
git merge [branchName] Merge changes from one branch to another
git rm [fileName] Remove file and record
git mv [fileName] Move file and version history within repository
git reset Remove any staged/added files
git diff [fileName] Check for any changes in a file compared to when the file last had a commit associated with it
git add --patch Select a particular change within a file to stage/add and commit
git --help Get help on any git command
git grep [pattern] Search inside all files in repository for pattern



Where do we type these git commands?!

Using git in the terminal

Using git in GUIs

Using git in GUIs - RStudio

Using git in GUIs - RStudio

Using git in GUIs - VSCode

Using git in GUIs - VSCode

Using git in GUIs - VSCode

Using git in GUIs - GitHub Desktop



How many of us have used GitHub before?

What is GitHub?

GitHub is a cloud-based platform for hosting git repositories


Features include:

  • Pull Requests: Propose changes, review, and discuss before merging branches.
  • Issues and Projects: Track bugs, tasks, and enhancements.
  • Actions: Automate workflows eg. testing, deployment & CI/CD.
  • Pages: Host websites directly from repositories.

GitHub - pull requests


  • Sharing changes for review
  • Key for collaborative coding
  • A pause before finalising changes

GitHub - issues and projects


  • Issues are any tasks for your project/codebase
  • Projects boards help to order and monitor tasks

GitHub - actions

  • Continuous integration and continuous delivery (CI/CD) tool
  • Automate building, testing, publishing processes
  • Ran on virtual machines remotely
name: Python package build

on:
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v3
      with:
        python-version: 3.11
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Test with unittest
      run: |
        python -m unittest

GitHub - pages


  • Host a website for you/your organisation/or project
  • Public and free

git security

  • .gitignore what should git ignore (like data or outputs)
  • Never put sensitive content (passwords, API keys, and individual details) in code (see guidance if you do)
  • precommit/Actions workflows can automatically check code
  • Manage who can access/edit your repositories on GitHub
# Ignore data and output folders
data/*
outputs/*

# Ignore some standard data formats
*.csv
*.xlsx
*.ods

# Ignore some general output formats
*.html
*.pptx
*.odp
*.png
*.tif
*.jpeg
*.odj

# Ignore some general report formats
*.docx
*.pdf
*.odt

# Ignore jupyter notebooks
*.ipynb

git pre-commit


Check out pre-commit hooks as a way to automatically run checks

git pre-commit


Check out pre-commit hooks as a way to automatically run checks


repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks: # info on support hooks here: https://pre-commit.com/hooks.html
    -   id: check-yaml # checks .yaml file formatting
    -   id: end-of-file-fixer # adds empty line at end of file
    -   id: trailing-whitespace # removes extra white space
    -   id: check-added-large-files # doesn't allow files more than 500Kb (configurable)
        args: ['--maxkb=5000']
    -   id: detect-private-key # detects the presence of private keys

Useful resources