git-academy



git-academy

7 2


git-academy

The Academy intro to git but in the new Catalyst branded slides.

On Github catalyst-training / git-academy

Catalyst

Git for the Academy

Presented by Julia and Evan

Administrivia

  • Follow along as we go
  • Any questions just shout

Special Thanks

  • Ian - Most of the organising for Academy
  • Alison - Gets training like this funded
  • Matthew Gray - Slides the first
  • Robert Higgins - Slides the second

Hands up

Hands up. Who has heard of Git? Who has used Git? Who has heard of Github? (or Gitlab or Bitbucket or ...) Who has an account on Github? Who knows what a pull request is?

What is Git?

  • Git is revision control for files
  • What is revision control?
  • Revision control is like the save button for your work
  • ...on steriods!
Git = Revision control Revision control = hyper save

With revision control, you can...

  • Share your work
  • Recover saved work
  • Backup your stuff

Because sometimes...

Git can save you

How do I use Git for my work?

  • Runs on Linux, Mac OSX and Windows
  • Git tracks an entire directory of files
  • Text files are best (like html, css)
  • Binary files can work to (like docx, odt, jpg, gif)
  • Pretty much anything you give it

How does it work?

  • Metadata is all kept in the .git folder
  • You store your 'saves' in a commit
  • Sharing work is done through URLs

So Let's Dive In!

Setup your Git

Open a shell, and...

# Install Git
sudo apt-get install git

# Configure Git, stores these in ~/.gitconfig
git config --global user.name "my full name"
git config --global user.email "your_email@example.com"
git config --global core.editor vim
git config --global color.ui auto

Your first commands

  • git init <-- Create a new repository
  • git add . <-- Get ready to save my work
  • git commit <-- Save 'added' work in a commit
  • git status <-- Tell me what's going on
  • git log <-- Show changes that goit me here

Create your repo

# Create a new homework repository
mkdir homework
cd homework
git init

git status

Should show something like...

trainer@academy ~ $ mkdir homework
trainer@academy ~ $ cd homework
trainer@academy ~/homework $ git init
Initialized empty Git repository in /home/trainer/homework/.git/
trainer@academy ~/homework $ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

Create some files

touch lab1.txt
touch lab2.txt
git status

Should show something like...

trainer@academy ~/homework $ vim lab1.txt
trainer@academy ~/homework $ vim lab2.txt
trainer@academy ~/homework $ git status
On branch master

Initial commit

Untracked files:
(use "git add ..." to include in what will be committed)

lab1.txt
lab2.txt

nothing added to commit but untracked files present (use "git add" to track)

Add files ready to commit

git add .
git status

Should show something like...

trainer@academy ~/homework $ git add .
trainer@academy ~/homework $ git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached ..." to unstage)

new file:   lab1.txt
new file:   lab2.txt

Commit your work

git commit

Puts you into a text editor requesting a commit message

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
#»······new file:   lab1.txt
#»······new file:   lab2.txt
#

Type something meaningful, like "Finished lab 1 + lab 2"

When you're ready, save and quit with :wq

Check out the log

git log

Should show something like...

trainer@academy ~/homework $ git log
commit 93b4e770e61aaf1b0b7b20cb2b69c4a99278a664
Author: Julia Larsen 
Date:   Tue Jan 12 01:30:52 2016 +1300

Finished lab 1 + lab 2

Note, because this is small, it wont open in a pager

Those commands again

  • git init <-- Create a new repository
  • git add . <-- Get ready to save my work
  • git commit <-- Save 'added' work in a commit
  • git status <-- Tell me what's going on
  • git log <-- Show changes that lead up to my current state

More

  • git show <-- Show me changes I just commited
  • git diff <-- What unsaved changes have I made?
  • git checkout . <-- Throw away my unsaved changes

Lets try these commands

git status
git show

should give...

trainer@academy ~/homework $ git status
On branch master
nothing to commit, working directory clean

trainer@academy ~/homework $ git show
commit 93b4e770e61aaf1b0b7b20cb2b69c4a99278a664
Author: Julia Larsen 
Date:   Tue Jan 12 01:30:52 2016 +1300

Finished lab 1 + lab 2

diff --git a/lab1.txt b/lab1.txt
new file mode 100644
index 0000000..b1a17ba
--- /dev/null
+++ b/lab1.txt
@@ -0,0 +1 @@
+old stuff
+old stuff
+old stuff
diff --git a/lab2.txt b/lab2.txt
new file mode 100644
index 0000000..d6d6d41
--- /dev/null
+++ b/lab2.txt
@@ -0,0 +1 @@
+old stuff
+old stuff
+old stuff

Edit your files

git diff
vim lab1.txt

# Check what changed
git status
git diff

Should show something like...

trainer@academy ~/homework $ git diff

trainer@academy ~/homework $ vim lab1.txt
trainer@academy ~/homework $ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)

modified:   lab1.txt

no changes added to commit (use "git add" and/or "git commit -a")

trainer@academy ~/homework $ git diff
diff --git a/lab1.txt b/lab1.txt
index b1a17ba..f55c2ce 100644
--- a/lab1.txt
+++ b/lab1.txt
@@ -1 +1,2 @@
old stuff
+new stuff
old stuff
-removed stuff
old stuff

Throw away your changes

git status
git checkout .
git status

Should give something like...

trainer@academy ~/homework $ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)

modified:   lab1.txt

no changes added to commit (use "git add" and/or "git commit -a")

trainer@academy ~/homework $ git checkout .
trainer@academy ~/homework $ git status
On branch master
nothing to commit, working directory clean

Commands so far

Change things: create, save, reset

  • git init <-- Create a new repository
  • git add . <-- Get ready to save my work
  • git commit <-- Save 'added' work in a commit
  • git checkout . <-- Throw away my unsaved changes

Commands so far

Check things: state, history, changes

  • git status <-- Tell me what's going on
  • git log <-- Show changes that lead up to my current state
  • git show <-- Show me changes I just commited
  • git diff <-- What changes have I made?

How do I get at existing repos?

git clone < url >

http(s)://...

(s)ftp://...

git://...

file://...

Have you seen this octocat?

Github

Many projects host their code on Github

... But... Why Octocat?

Git has a command called octomerge

The internet is made out of cats

 

Octopus + Cat = Octocat!!

History time!

  • Git

    • Who: Linus Torvalds
    • When: ~04-2005 (~10 years old)
    • Where: git-scm.com
    • Why: Frustration with Bitkeeper
  • Github

    • Who: Tom + Chris + PJ
    • When: 04-2008 (~7 years old)
    • Where: github.com
    • Why: Commercial interests

Go setup a Github account!

http://github.com

Git and Secure SHell (SSH)

The best way to interact with Github is using ssh

SSH uses public/private key pairs

Lets set that up quickly

# Generate SSH keys
ssh-keygen -t rsa -C "your_email@example.com"

# You should see them in ~/.ssh
ls -l ~/.ssh
cat ~/.ssh/id_rsa.pub

Take the text from ~/.ssh/id_rsa.pub and put it into Github

Cool! Lets build something!

Some kind of website?

Can Github host those?

Of course

To make this website, we'll be setting up a new repo

Go here --> http://pages.github.com

  • Select "User or organisation site"
  • Your Git client is "A terminal"

Two more commands

# For multiple checkouts or shared work
git pull

# After commiting, push those changes to github
git push

Commands so far

Change things: create, save, reset

  • git init <-- Create a new repository
  • git clone <-- Copy an existing repository
  • git add . <-- Get ready to save my work
  • git commit <-- Save 'added' work in a commit
  • git checkout . <-- Throw away my unsaved changes

Commands so far

Check things: state, history, changes

  • git status <-- Tell me what's going on
  • git log <-- Show changes that lead up to my current state
  • git show <-- Show me changes I just commited
  • git diff <-- What changes have I made?

Commands so far

Share your changes

  • git pull <-- Get changes from cloned repo
  • git push <-- Push changes up to a repo

Now what?

Now go use Git!

All the Academy projects are hosted in Git

Find the coding guidelines Clone the code, hack on it, commit your work Submit your work...

Submit how?

  • Drupal: Attach commits to 'issues' as patches
  • Koha: Attach commits to 'bugs' as patches
  • Silverstripe: Pull Request via Github
  • Piwik: Pull Request via Github
  • Mahara: Submit patches via Gerrit review system

Thanks

These slides are on Github

If you have any feedback, please raise an Issue

Go forth and be awesome!

open source technologists

Catalyst Git for the Academy Presented by Julia and Evan