+ - 0:00:00
Notes for current slide
Notes for next slide

Getting Git

Sean Hardison
Ecosystem Dynamics and Assessment
Integrated Statistics & Northeast Fisheries Science Center

1 / 27

Today's workshop

  • Version control systems + Git

  • Concepts in Git

  • Integration with Github

  • Create, collaborate, and share

Source material

Available here

Chacon, Scott, and Ben Straub. Pro git. Apress, 2014.

2 / 27

Why use Github and Git?

  • For science:
    • Reproducibility
    • Transparency
    • Ease of collaboration
    • Communicating your work
  • It just works.
    • Never lose a file
    • Work from multiple machines seemlessly

Image source: twitter.com/Linux/status/936877536780283905/photo/1

3 / 27

Rep - With Github, anyone with the right tools should be able to recreate your workflow Collabora

Your friendly neighborhood Version Control System 1:

  • Local Version Control (L-VC):

    • Developed in response to error-prone manual VC

    • A database keeping track of file changes ("patches")

    • Patches can be added and subtracted to revert to documents in a certain state

4 / 27

Your friendly neighborhood Version Control System 2:

  • Centralized Version Control (CVC):

    • Allows collaboration between systems

    • Files can be checked out from central server hosting database of file patches

    • Centralized server is weakness: If server goes down, VC/collaboration cannot occur

5 / 27

Your friendly neighborhood Version Control System 3:

  • Distributed Version Control (DVC):

    • Users check out copies of full directories rather than individual files

    • These directories, or repositories, include the full history of each file

    • In short: everyone has the database of patches

6 / 27

Distributed Version Control Systems

  • Distributed Version Control (DVC):

    • A copy you create of a remote repository is called a clone

clone-wars

  • Git is a distributed version control software
7 / 27

Vocabulary check

  • Repository: A file directory that is being tracked by Git

    • Also called a "repo"
  • Clone: An exact copy of a repository containing the full history of project changes

8 / 27

How does Git work?

  • Git stores information about the state of all files in the repository (think repo "snapshots")

  • Files that have not been changed are not replicated, but linked to

  • All information is local, and all operations can be performed offline

9 / 27

When a snapshot is taken, a file is assigned an id specific to the checksum, or hash. No further changes can be made to that file without Git first knowing about it, and so you cannot lose information.

The Three File States

  • Files can be in one of three states after being added to repo

    1. Staged - Files are marked for committing (the snapshot)

    2. Modified - Staged files have been modified, but not committed

    3. Committed - Snapshot taken, file states stored to local database

10 / 27

The Three File States continued

  • Repository snapshots are transferred between local and remote sessions using push and pull commands

11 / 27

Actually Getting Git: User set-up

Local Git

  1. Open command line or terminal window

  2. Check user name and email address

    git config --global user.name

    git config --global user.email

  3. Check text editor

    git config --global core.editor

  • Important: Your git email/username must map to your Github account

GitHub (remote Git)

  1. Create Github account if you have not done so already

  2. Log in!

12 / 27

Important that user name and email map to what you've used to sign up with on github

On windows, you will need to specify the full path to the executable file if changing text editor

Groups & Remote (Github) repository creation

  1. Break out into groups of 3 or 4

  2. Nominate group leader

13 / 27

Groups & Remote (Github) repository creation

  1. Break out into groups of 3 or 4

  2. Nominate group leader

  3. Create a getting-git repository on Github (gif below)

  4. Group leaders: Allow group members to collaborate: Settings -> Collaborators -> [usernames]

create_gh_repo

13 / 27

Cloning a repository

From RStudio

  1. Open RStudio and select File -> New Project

  2. Select Version Control

  3. Select Git

  4. Paste https://github.com/[leader_username]/getting-git into repo URL

  5. Name your project getting-git

From cmd/terminal

  1. Open command line or terminal

  2. cd into a directory where you want project to be housed

  3. Enter git clone https://github.com/[leader_username]/getting-git

14 / 27

RStudio will automatically assign an Rproject file to your repository.

Vocabulary check

  • Commit: Git for "file save"

  • Remote: A repository not located on your local machine

    • The repository you just cloned is "the remote"
15 / 27

Create, commit, pull, and push

In RStudio

  1. Create a new file in /getting-git (.R, .txt, etc)

  2. Stage your file

  3. Commit changes

  4. Pull to download latest snapshot

  5. Push to remote repository (Github)

16 / 27

Create, commit, pull, and push

In RStudio

  1. Create a new file in /getting-git (.R, .txt, etc)

  2. Stage your file

  3. Commit changes

  4. Pull to download latest snapshot

  5. Push to remote repository (Github)

add_commit_push

16 / 27

Create, commit, pull, and push

From command line/terminal

  1. Create new file in /getting-git

  2. Stage your file:

    • git add [filename.xyz]
  3. Commit changes

    • git commit or git commit -m "[commit message]"
  4. Pull to download latest snapshot

    • git pull
  5. Push to remote repository (Github)

    • git push
17 / 27

The Great Merge Conflict of 2019

git-merge

  • Merge conflicts occur when collaborators overwrite each other's content

  • A merge conflict must be fixed manually

18 / 27

The Great Merge Conflict of 2019

git-merge

  • Merge conflicts occur when collaborators overwrite each other's content

  • A merge conflict must be fixed manually

  • Now for a well-orchestrated example

18 / 27

The Great Merge Conflict of 2019 (continued)

In your groups, force a merge conflict and work through it.

In someone else's document

  1. Edit another group member's document

  2. Stage, commit, and push the change

19 / 27

The Great Merge Conflict of 2019 (continued)

In your groups, force a merge conflict and work through it.

In someone else's document

  1. Edit another group member's document

  2. Stage, commit, and push the change

In your document (edited by someone else)

  1. Make a change overlapping with group member edits. Stage and commit.

  2. Pull from Github: a merge conflict will appear.

  3. Fix conflict, stage, commit, and push!

19 / 27

The Great Merge Conflict of 2019 (continued)

In your groups, force a merge conflict and work through it.

In someone else's document

  1. Edit another group member's document

  2. Stage, commit, and push the change

In your document (edited by someone else)

  1. Make a change overlapping with group member edits. Stage and commit.

  2. Pull from Github: a merge conflict will appear.

  3. Fix conflict, stage, commit, and push!

In summary: Merge conflicts are a natural part of getting git

19 / 27

Fixing conflicts at the command line/terminal

Assuming conflict exists between remote and local repos:

  1. Add changes: git add [filename.xyz]

  2. Commit changes: git commit -m "[going about my own business]"

  3. Pull remote to prompt merge conflict: git pull

  4. Fix conflict

  5. Add changes, commit, and push!

20 / 27

Sharing your work through Github

Github Pages is a free service for hosting repository webpages

  • Hugely flexible; allows for interactivity, JS library integration
  • Easy to use

Some examples from EDAB:

21 / 27

Let's make something to share with the world!

Group leaders: Go to your repository setting and turn on Github Pages

init-gh-pages

22 / 27

Let's make something to share with the world!

  1. Open up a new Rmarkdown document

  2. Save it as "index.Rmd"

  3. Knit the document to HTML

  4. Stage, commit, and push index.html and index.Rmd to Github

  5. Go to the url: [leader_username].github.io/[repository_name]

23 / 27

Let's make something to share with the world!

  • Once an index.html file has been push to the repo, more .html files can also be hosted

    • [username].github.io/[repository_name]/[paradigm_shifter.html]
  • Interactivity is easy:

library(dygraphs)
dygraph(nhtemp, main = "New Haven Temperatures") %>% dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))

New Haven Temperatures
50
55
1920
1930
1940
1950
24 / 27

Branching

  • Ensures main repository (master branch) is "production quality"

  • Can be used to isolate breaking changes in code for testing, then merged into master
25 / 27

Helpful git commands

Add all files: git add .

Commit w/o text editor: git commit -m "Your commit message"

Show all settings: git config --list

View status of files in git repo: git status

View unstaged changes: git diff

View staged changes git diff --staged

View commit history git log (pretty: git log --pretty=format:"%h - %an, %ar : %s")

Revert to commmit: git checkout [commit-ref] [filename.xyz]

26 / 27

Creating a local repository

From RStudio

  1. Open RStudio and select File -> New Project

  2. Select New Directory

  3. Select New Project

  4. Enter directory name and location

  5. Select Create a git repository

From cmd/terminal

  1. Open command line or terminal

  2. cd into a directory where you want project to be housed

  3. Create folder for project mkdir [folder-name]

  4. cd into new directory

  5. Initialize repository git init

  6. Link to GitHub

27 / 27

Today's workshop

  • Version control systems + Git

  • Concepts in Git

  • Integration with Github

  • Create, collaborate, and share

Source material

Available here

Chacon, Scott, and Ben Straub. Pro git. Apress, 2014.

2 / 27
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow