Introduction to git and github#

Peer Herholz (he/him)
Postdoctoral researcher - NeuroDataScience lab at MNI/McGill, UNIQUE
Member - BIDS, ReproNim, Brainhack, Neuromod, OHBM SEA-SIG

logo logo   @peerherholz

Before we get started …#


  • most of what you’ll see within this lecture was prepared by Kendra Oudyk and further adapted by Peer Herholz

  • based on the Software Carpentries “Version control with Git” under CC-BY 4.0

Roadmap#

  • Goals

  • Setup

  • Why use git & GitHub?

  • Where does git store information?

  • How do I record changes in git?

  • How do I share my changes on the web?

  • How do I contribute to an existing project?

  • Goals

Goals#

  1. Explain why git/GitHub are useful

  2. Track and share your work using git/GitHub (git level 1: commit push)

  3. Contribute to a project using git/GitHub (git level 2: branches PRs)

On learning git & GitHub#

Roadmap#

  • Goals

  • Setup

  • Why use git & GitHub?

  • Where does git store information?

  • How do I record changes in git?

  • How do I share my changes on the web?

  • How do I contribute to an existing project?

  • Goals

Import side note:#

throughout this lecture/practice you will see a lot of things in <>, within each instance please replace <> with your own info

e.g.,

github.com/<your_username>

becomes

github.com/peerherholz

Setup#

To follow on your machine, you’ll need#

  1. Bash

  2. Git

  3. Text editor

  4. GitHub account

Check if you’re ready#

  1. Can you open a text editor? (e.g., Linux: gedit, nano. macOS: textedit. Windows: notepad)

  2. Can you go to your GitHub account?

  3. When you open a Bash shell and type git --version, does it output the version number? (macOS / Linux: you might need to run this: conda install -c anaconda git)

Configure git (if you haven’t already)#

git config --global user.name "<Vlad Dracula>"
git config --global user.email "<vlad@tran.sylvan.ia>"

use the email you used for your GitHub account 👆

macOS / Linux#

git config --global core.autocrlf input

Windows#

git config --global core.autocrlf true

Roadmap#

  • Goals

  • Setup

  • Why use git & GitHub?

  • Where does git store information?

  • How do I record changes in git?

  • How do I share my changes on the web?

  • How do I contribute to an existing project?

  • Goals

git-phd

“Piled Higher and Deeper” by Jorge Cham, http://www.phdcomics.com

Why use git & GitHub?#

Automated version control

Record versions by tracking changes#

It’s like having an unlimited “undo” button

Make independent changes#

And incorporate the changes#

https://swcarpentry.github.io/git-novice/

Roadmap#

  • Goals

  • Setup

  • Why use git & GitHub?

  • Where does git store information?

  • How do I record changes in git?

  • How do I share my changes on the web?

  • How do I contribute to an existing project?

  • Goals

Open your Bash shell (where you typed git --version at the beginning)

Create a directory (remember Windows’ slashes are the other way)

cd ~/Desktop
mkdir desserts
cd desserts

What’s in our directory?

ls -a

Create a git repository

git init

What’s in our directory now?

ls -a

The .git subdirectory is where git stores all the info it needs to do version control

Roadmap#

  • Goals

  • Setup

  • Why use git & GitHub?

  • Where does git store information?

  • How do I record changes in git?

  • How do I share my changes on the web?

  • How do I contribute to an existing project?

  • Goals

git add#

git commit#

Let’s make a change! First, open a new file

<text editor> desserts.md

Write this in the file:

pie
ice cream
cookies

Save and exit

Reminder to let me know if you need me to slow down

Let’s check the status of our repo

git status

Is this file being tracked by git?

(hint: look at what your terminal says)

How can we include this file in what will be committed?

Let’s stage the change

git add desserts.md

Let’s check the status of our repo

git status

Let’s commit the change

git commit -m "list my favorite desserts"

Let’s check the status of our repo

git status

I change my mind…

cookies are better than ice cream

$ <text editor> desserts.md

pie
cookies
ice cream

Save and exit

Let’s ________________________

git diff

How could we figure out what this command does?

Let’s stage and commit the change

git ____ desserts.md
git ____ -m "switch cookies and ice cream"

Check your understanding#

What does git track?#

Does git track changes to each letter?#

How do I get Git to track a change?#

Put these in order:

a) git commit -m "<this is what I did>"
b) make the change
c) git add <file>

Roadmap#

  • Goals

  • Setup

  • Why use git & GitHub?

  • Where does git store information?

  • How do I record changes in git?

  • How do I share my changes on the web?

  • How do I contribute to an existing project?

  • Goals

Create a remote repo#

  • Beside Repositories, click New

  • Enter your repo name

  • Choose to make your repo Public or Private

  • Don’t check any boxes

  • Click Create repository

Roadmap#

  • Goals

  • Setup

  • Why use git & GitHub?

  • Where does git store information?

  • How do I record changes in git?

  • How do I share my changes on the web?

  • How do I contribute to an existing project?

  • Goals

Branches#

I want to contribute!#

Contributing task 1: Get everyone’s favorite desserts!#

Roadmap#

  • Goals

  • Setup

  • Why use git & GitHub?

  • Where does git store information?

  • How do I record changes in git?

  • How do I share my changes on the web?

  • How do I contribute to an existing project?

  • Goals

Did we meet our goals?#

1. Explain why git & GitHub are useful#

… to a new grad student

2. Track and share your work using git/GitHub (git level 1: commit push)#

status     add     init     commit     diff     push    

Basic workflow for tracking a change and putting it on GitHub

  • make a change

  • stage the change: git ____ <filename>

  • commit the change: git ____ -m "<commit message>"

  • put the change on GitHub: git ____ origin main

See what’s happening with git

  • show the working tree status: git ____

  • show how the file changed: git ____

3. Contribute to a project using git/GitHub (git level 2: branches PRs)#

Contributing task 2: Correct spelling mistakes in desserts lists#

There’s so much more!#

Git buffet#

Git is hard#

Here are some tips

Sit down and go through a tutorial#

Don’t expect to remember everything#

Keep common commands on a sticky note#

To learn it, you need to commit to doing it#

Quick feedback#

How much of this tutorial could you follow?#

  • 100 %

  • 75 %

  • 50 %

  • 25 %

  • 0 %

Where are there any major hurdles?#

The End#

Software Carpentry’s tutorial: https://swcarpentry.github.io/git-novice/