1 of 24

Git and GitHub

Luke Marsden, UNIS

(data.nleg@unis.no)

2 of 24

What is version control and why use it?

shopping_list.txt

  • Both can add to or remove things from list
  • Don’t override each others changes
  • Track who has added what
  • Both work on list at same time
  • Revert to old versions

Max

Tom

3 of 24

Why use GitHub?

shopping_list.txt

  • Both can edit from their own offices
  • Adam can also use the list
  • Adam can chip in with suggestions
  • Adam’s suggestions must be approved by Max or Tom

*Adam could be anyone

Max

Tom

Adam

4 of 24

Downloading Git

5 of 24

Using Git Bash

6 of 24

Create repository

1. From GitHub

7 of 24

Create repository

  • Create local directory and navigate into it in your terminal
  • Clone the git repo in your local directory. Copy & paste URL of repo. Paste is middle mouse button in Git Bash.
  • Navigate into repository.

1. From GitHub

shoppinglist

shopping_list.txt

README.md

8 of 24

Create repository

2. Locally and

upload

  • Create local directory and files (or maybe you already have one you want to use).
  • Create empty GitHub repository (as previous)
  • Initialise the directory as a git repository
  • Add the files to be committed
  • Commit the files that you have staged
  • Link repo to ‘remote’ repo at GitHub
        • Push repo to GitHub

shopping_list.txt

shoppinglist2

README.md

9 of 24

Create repository

2. Locally and

upload

10 of 24

Commits

  • Commit is a snapshot of repository
  • Repo contents, author, timestamp…

C0

C1

Added pizza to shopping list

main*

Initial commit

shopping_list.txt

shoppinglist

README.md

11 of 24

Pushing

  • Local main branch ahead of remote master branch (on GitHub)

shopping_list.txt

shoppinglist

README.md

C0

C1

local main*

remote main

12 of 24

ID of commit

ID of parent commit

Branch name

13 of 24

Making changes on GitHub

14 of 24

Pulling

  • Update local repo with remote changes
  • ‘pull’ the latest version from GitHub.
  • Pull often for updates

shopping_list.txt

shoppinglist

README.md

C0

C1

local main*

remote main

C2

15 of 24

Branches

shopping_list.txt

shoppinglist

README.md

C0

C1

C2

main*

newBranch

  • Branch is pointer to a commit
  • New branch to work outside of main work flow – common practice if multiple users
  • Checkout new branch
  • Can create new branch and checkout 1 line

C3

main

newBranch*

16 of 24

Merging branches

  • Working on newBranch, updated file and committed changes.
  • Checkout branch you want to merge into
  • Merge newBranch so changes on main
  • Delete branch if no longer needed
  • Push changes

C0

C1

C2

shopping_list.txt

shoppinglist

README.md

C3

main

newBranch*

main*

newBranch

C4

17 of 24

Two people working at same time

  • Two people checkout new branches
  • Both branches have new commits
  • Checkout main and merge newBranch1
  • Try merge newBranch2, conflicts with main
  • git status, git log can help, then open file
  • Fix conflicts then add and commit

C0

C1

C3

C2

main*

newBranch1*

newBranch2*

main

newBranch1*

newBranch2*

main

newBranch1*

main

main

newBranch2*

C4

newBranch1

main*

newBranch1

newBranch2

From newBranch2

From main (HEAD denotes current branch)

Common to both

main

C5

newBranch2

18 of 24

Diff

  • Forgot what changes you’ve made
  • Check changes made since last commit

19 of 24

Pull requests

  • Best practice for documenting and reviewing changes
  • Pull, create new branch
  • Make changes, add, commit
  • Push – new branch appears on GitHub

C0

C1

local main*

main

main

newBranch*

local

remote

C2

main

newBranch*

newBranch

20 of 24

Pull requests

  • Best practice for documenting and reviewing changes
  • Pull, create new branch
  • Make changes, add, commit
  • Push – new branch appears on GitHub
  • Create pull request – to merge branches
  • Someone else reviews pull request

21 of 24

Forks and pull requests

  • Working on a repo that isn’t yours.
  • Forking creates copy of repo.
  • Clone forked repo in git bash.
  • Checkout a new branch.
  • Make, add, commit and push changes.

22 of 24

Forks and pull requests

  • Working on a repo that isn’t yours.
  • Forking creates copy of repo.
  • Clone forked repo in git bash.
  • Checkout a new branch.
  • Make, add, commit and push changes.
  • Go to GitHub, fork repo, submit pull request
  • Owner of parent repo reviews pull request.

23 of 24

What to include in a README

  • Project name

  • Short description of project

  • Contents of repository with descriptions

  • Instructions on how to install and use your scripts/software

  • Credits – who is involved?

24 of 24

Final remarks

Pull from origin (GitHub) before you start to update your local repository.

Push and pull regularly, especially if multiple users of repository.

Don’t work on main branch, create new one then merge or submit pull request.

This is just an introduction

There is SO much help online

        • Stack Overflow
    • Youtube (e.g. this playlist by ‘The Net Ninja’)
    • Google

My favourite visual, interactive way to learn git branching and version control: