How to use git

For individual projects

Andy Balaam
artificialworlds.net/blog

Contents

Credits

Heavily inspired by Joel Spolsky's "Hg Init".

hginit.com

Starting a new project

What's first?

Starting a new project

Write some code

$ mkdir sayings
$ cd sayings
$ vim northern.txt
northern.txt
Ay up
Were tha born in a barn?

Starting a new project

Create an empty project

$ git init
Initialized empty Git repository in /home/andy/sayings/.git/

Starting a new project

Ask git what's happening

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	northern.txt
nothing added to commit but untracked files present (use "git add" to track)

Starting a new project

Add the file to source control

$ git add northern.txt

Starting a new project

The file is "staged"

$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   northern.txt
#

Starting a new project

Finish the change

$ git commit
.git/COMMIT_EDITMSG
First commit: started with some northern.|
# 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:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   northern.txt
#

Starting a new project

The commit worked

$ git commit
# On branch master
[master (root-commit) 813932a] First commit: started with some northern.
 1 file changed, 3 insertions(+)
 create mode 100644 northern.txt
$ git status
# On branch master
nothing to commit (working directory clean)

Making some changes

Then you do stuff

Making some changes

Add a file, modify one

$ vim southern.txt
southern.txt
Cor blimey
Oi oi

Making some changes

Add a file, modify one

$ vim northern.txt
northern.txt
Ay up
Ee by gum
Were tha born in a barn?

Making some changes

Ask git what's going on

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   northern.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	southern.txt
no changes added to commit (use "git add" and/or "git commit -a")

Making some changes

Check what you changed

$ git diff northern.txt
diff --git a/northern.txt b/northern.txt
index bbb4a54..993a13e 100644
--- a/northern.txt
+++ b/northern.txt
@@ -1,3 +1,4 @@
 Ay up
+Eey by gum
 Were tha born in a barn?

Making some changes

Tell git about your new file

$ git add southern.txt

Making some changes

Ask git what's going on

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   southern.txt
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   northern.txt
#

Making some changes

Tell git you changed a file

$ git add northern.txt

Making some changes

Ask git what's going on

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   northern.txt
#	new file:   southern.txt
#

Making some changes

Finish the change

$ git commit
.git/COMMIT_EDITMSG
Added some southern, and a new northern.|
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   northern.txt
#       new file:   southern.txt
#

Making some changes

The commit worked

$ git commit
[master 9af3c75] Added some southern, and a new northern.
 2 files changed, 4 insertions(+)
 create mode 100644 southern.txt
$ git status
# On branch master
nothing to commit (working directory clean)

Examining history

Why, why, why?

Examining history

What happened before?

$ git log
commit c5ec6fd107c540d4e21c0f8ef09678cf3922edbf
Author: Andy Balaam <andybalaam at artificialworlds.net>
Date:   Fri Mar 15 10:36:43 2013 +0000

    Added some southern, and a new northern.

commit de27edc0c3667569ce7736de376e1787b1284208
Author: Andy Balaam <andybalaam at artificialworlds.net>
Date:   Fri Mar 15 10:33:45 2013 +0000

    First commit: started with some northern.

Examining history

Details

$ git show de27
commit de27edc0c3667569ce7736de376e1787b1284208
Author: Andy Balaam <andybalaam at artificialworlds.net>
Date:   Fri Mar 15 10:33:45 2013 +0000

    First commit: started with some northern.

diff --git a/northern.txt b/northern.txt
new file mode 100644
index 0000000..bbb4a54
--- /dev/null
+++ b/northern.txt
@@ -0,0 +1,3 @@
+Ay up
+Were tha born in a barn?

Interrupting (stash)

STOP AND DO WHAT I WANT

Interrupting (stash)

Get stuck into some work

$ vim southern.txt
southern.txt
Cor blimey
Oi oi
Awight lads
Ow's it goin?
Where to guvnor?
Me old China

Interrupting (stash)

Urgent customer issue!

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   southern.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

Interrupting (stash)

Just stash it

$ git stash
Saved working directory and index state WIP on master: 9af3c75 Added some southern, and a new northern.
HEAD is now at 9af3c75 Added some southern, and a new northern.
$ git status
# On branch master
nothing to commit (working directory clean)

Interrupting (stash)

Make the fix

$ vim southern.txt
southern.txt
Gor blimey
Oi oi

Interrupting (stash)

Check in the fix

$ git add .
$ git commit -m "Fixed urgent C/G issue"
[master 12c3148] Fixed urgent C/G issue
 1 file changed, 1 insertion(+), 1 deletion(-)

Interrupting (stash)

Go back to what you were doing

$ git stash list
stash@{0}: WIP on master: 9af3c75 Added some southern, and a new northern.

Interrupting (stash)

Go back to what you were doing

$ git stash pop
Auto-merging southern.txt
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   southern.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (b6ac27b97b3db493370545b7b5042c56fb9acb2f)

Interrupting (stash)

Go back to what you were doing

$ git diff southern.txt
diff --git a/southern.txt b/southern.txt
index 624bd2a..1d34fc0 100644
--- a/southern.txt
+++ b/southern.txt
@@ -1,3 +1,7 @@
 Gor blimey
 Oi oi
+Awight lads
+Ow's it goin?
+Where to guvnor?
+Me old China

More info

Videos youtube.com/user/ajbalaam
Twitter @andybalaam
Blog artificialworlds.net/blog
Projects artificialworlds.net