You are on page 1of 28

Advanced Git Tutorial

by Sarah Sharp
WARNING:
I am a unique snowflake
WARNING:
This tutorial may make you lazy
WARNING:
Some Git features are
dangerous!
Git Basics
● See Everyday Git Tutorial:
– http://www.kernel.org/pub/software/scm/git/d
ocs/everyday.html
● My git commands:
– git add - git commit
– git diff - git log - git show
– git push - git pull
– git fetch - git rebase
Setting up a
remote repository
Setting up a
remote repository
● Server needs git installed to use git+ssh
to push to your repo
● Server needs webdev installed to allow
pulls over http
● http://github.com/ will host your repo
● Next directions assume you have your
own server with git installed
Setting up a
remote repository
1. Make local repo, commit stuff, etc.
2. ssh to the server:
GIT_DIR=/path/to/repo git init --shared
3. Next, tell the local repo about the server:
git remote add origin git+ssh://hostname/path/to/repo
4. Push to the server from the local repo:
git push origin master
5. Clean up the local repo so that you can pull from the
remote server:
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
Git Philosophy
Git Philosophy
● Commit early, commit often
● One commit represents one idea or one
change.
– Makes it easy to read patches
– Easy to revert unwanted changes later
● Your working directory, index, and local
repo are your scratch pads.
Frequent Use Infrequent Use
git diff
● Only pays attention to tracked files
● git diff
– differences between workspace and index
● git diff --cached
– differences between index and local repo, i.e.
changes to be commited
● git diff HEAD
– differences between workspace and local
repo
Viewing History
● git log
● git log <commit ID>
– shows history before that commit
– e.g. git log HEAD
● git log -p
– shows log as a series of patches
The Index, the staging area

Front stage: Back stage:


Changes to be committed Uncommited changes
and unadded files
Staging Changes
● git add <file> – adds a file to the Index
● git commit – commits added changes to
the local repo
● But what about files not added to the
Index?
Staging Changes
● git add <file> – adds a file to the Index
● git commit – commits added changes to
the local repo
● But what about files not added to the
Index?
– Answer: they aren't included in the commit.
● Key idea: You can add and commit files
separately from other files.
– This makes separating changes into small
patches easier.
Staging Changes
● git add --patch
– try the "split" option to split across hunks
● git add -i
● Very powerful tool:
– add portions of a file
– revert changes you've added to the index
● Key idea: You can add and commit
different parts of a file separately.
Changing History
Changing History:
DANGER, WILL ROBINSON!
● After a commit, often you will find bugs
– could make a new bug fix commit
– or you could "amend" the previous commit
● Fix your code
● git add <file>
– This adds your code to the index
● git commit --amend
– This modifies the commit in the local repo
Changing History:
DANGER, WILL ROBINSON!
● A total history rewrite:
– git rebase -i <commit ID>
● Can reorder commits
● Can edit commits
● Can "squash" one commit into another
Git Hooks
Git Hooks
● Hooks are scripts found in .git/hooks/
● Enable them with chmod a+x <file>
● Triggered by various git commands
– e.g. git commit, git push
– pre-commit, post-update
● Examples
– shipped pre-commit hook checks for white
space at the end of line, long lines, etc.
– Checking for swear words?
Git Hooks
● Example post-update hook on remote repo:
#!/bin/sh
cd /home/sarah/blog
unset GIT_DIR
git-fetch origin
git-reset --hard origin/master

● Whenever I push to the remote repository, this


goes into the server's checkout of my blog git
repo and updates it unconditionally.
Pointing Fingers:
git blame
Pointing Fingers
● git blame <file>
– show who committed each line
● git blame <file> <commit ID>
– show the line history before that commit
Resources
● Git work flow diagrams:
http://osteele.com/archives/2008/05/my-git-workflow
● The Tangled Working Copy:
http://tomayko.com/writings/the-thing-about-git
● http://github.com/
● Kernel module examples at http://lwn.net/Kernel/LDD3/
Creative Commons
Image Attributions
● GIT picture:
http://flickr.com/photos/29862082@N06/2908889599/
● Snowflake:
http://commons.wikimedia.org/wiki/Image:SnowflakesWilson
Bentley.jpg
● Danger:http://flickr.com/photos/induel/624264382/
● Cat: http://flickr.com/photos/jamilsoni/118499378/
● Remote: http://flickr.com/photos/markkelley/957631507/
● Philosophy: http://flickr.com/photos/paullew/2442045767/
Creative Commons
Image Attributions
● Front stage:
http://flickr.com/photos/69108241@N00/118040089/
● Back stage: http://flickr.com/photos/piotramigo/2561391320/
● Hooks:
http://flickr.com/photos/curiousexpeditions/1569728934/
● Blame: http://flickr.com/photos/iandesign/1205496024/

You might also like