Introduction & Refresher
Overview
Teaching: 10 min
Exercises: 10 minQuestions
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
- git is a version control system which records snapshots of a project.
- We can navigate back and forwards in the history of the changes made to a repository.
- To record a snapshot in the repository, we need to add the changes we want to record to the staging area with
git add [filename], and then commit the changes withgit commit -m "useful message".
Challenge 1
Create a git repository named
git-refresher. Make sure it is not nested inside another repository. Make a text file calledbooks.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.txtonly 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
- We can have a local copy of a repository (on the disk of the computer we’re using), and/or a remote copy of a repository on a git hosting service like github or bitbucket.
- To make a local copy from a remote repository, use
git clone [repo-address] - To bring changes from a remote to the local repository, use
git pull - To send local changes to the remote, use
git push
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.txtoringredients.txtcreated 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
- A great visual way to play with git concepts: http://git-school.github.io/visualizing-git/
- A git cheat sheet https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf
- A game based intro to git: https://learngitbranching.js.org/
Key Points
git has a two stage process:
addandcommitWe can have local and remote repositories, and synchronise changes between them