Introduction & Refresher

Overview

Teaching: 10 min
Exercises: 10 min
Questions
  • How do I add changes to a git repository?

  • How do I work with a remote git repository?

Objectives
  • Remind ourselves how git works

Refresher

Challenge 1

Create a git repository named git-refresher. Make sure it is not nested inside another repository. Make a text file called books.txt. In three separate commits, add the titles of your favourite three books on separate rows.

Navigate back to the first commit, and check that books.txt only has a single title.

Solution

Note: There are many ways of adding content to a text file. Adding text directly through an editor (like Sublime) is fine. The solution below uses > and >> to add contents directly to the textfile from the command line so that the whole solution can be shown.

$ mkdir git-refresher
$ cd git-refresher
$ git init
$ echo "True Grit" > books.txt
$ git add books.txt
$ git commit -m "Initial commit - added first book title"
$ echo "The Princess Bride" >> books.txt
$ git add books.txt
$ git commit -m "added second book"
$ echo "The Mask of Dimitrios" >> books.txt
$ git add books.txt
$ git commit -m "added third book"

#To navigate back two commits we can check the log (your output will have different details)
$ git log
commit 337af2e62bc52b797b92612a7c35aa9f93b3f4d1 (HEAD -> master) 
author: Alex Whan <alexwhan@gmail.com>
date:   Thu Apr 4 14:33:01 2019 +1100

    added third book  

commit ef495bf15b38cb272633bc7d29284e6627e81eab 
author: Alex Whan <alexwhan@gmail.com> 
Date:   Thu Apr 4 14:32:37 2019 +1100

    added second book  
    
commit 235365a0df7c7d8b9213dd49ab9e998db507cc38 
Author: Alex Whan <alexwhan@gmail.com> 
Date:   Thu Apr 4 14:32:07 2019 +1100

    Initial commit - added first book title

$ git checkout 235365a0
$ cat books.txt

Reminder - you can get back to where you were with `git checkout master`

Reminder

We can always check on the state of the git repository with git status. This will show which files are untracked, which have changed, and whether they are staged or not.

Working with remotes

Challenge 2

Clone a local copy of the git-guacamole repo. Make sure you think about where you should make your local copy.

  • How many commits have been made in the repository?
  • Who made them?
  • Was instructions.txt or ingredients.txt created first?

Solution

Make sure you are not inside a git repository when you do git clone

$ git clone https://github.com/csiro-data-school/git-guacamole
$ cd git-guacamole
$ git log

# One possible solution:
$ git log --oneline | wc -l

Resources

Key Points

  • git has a two stage process: add and commit

  • We can have local and remote repositories, and synchronise changes between them