Git Crash Course

Presenter Notes

Git is...

  • a collection of commits that represent code changes
  • distributed (redundant)
  • flexibile
  • network optional

Presenter Notes

can be a little intimidating

Presenter Notes

a commit is...

  • snapshot, not a diff
  • referred to by a checksum (not a name)
  • takes up more space, but bits are cheap
  • store links to files that haven't changed

Presenter Notes

stop worrying

  • Git (generally) only adds data
  • it's hard to lose things once committed
  • almost always possible to roll back changes
  • other than 'fetch', 'push', and 'pull', nearly everything is local

Presenter Notes

three (or four) stages of Git

Presenter Notes

basic workflow

  1. make some changes

  2. stage snapshots of files

  3. commit

  4. (eventually) push

Presenter Notes

a commit has...

  • a checksum
  • all blobs contained in the commit
  • a comment (please use these)

Format your commit like:

80 character summary

longer description of
changes, maybe include a bulleted
list of changes you made

Presenter Notes

getting set up

1 git config --global user.name "Your Name"
2 git config --global user.email "you@your.site"

Presenter Notes

status report

 1 $ git status
 2  # Initial commit
 3  #
 4  # Changes to be committed:
 5  #   (use "git rm --cached <file>..." to unstage)
 6  #
 7  #       new file:   README
 8 $ vim README
 9 $ git status
10  # Initial commit
11  #
12  # Changes to be committed:
13  #   (use "git rm --cached <file>..." to unstage)
14  #
15  #       new file:   README
16  #
17  # Changes not staged for commit:
18  #   (use "git add <file>..." to update what will
19  #   be committed)
20  #   (use "git checkout -- <file>..." to discard
21  #   changes in working directory)
22  #
23  #       modified:   README

Presenter Notes

making commitments

1 $ git commit -m "Added README"
2 [master (root-commit) 4f3ea33] Added README
3  1 files changed, 1 insertions(+), 0 deletions(-)
4  create mode 100644 README
5  <Do more commits>
6 $ git log --oneline
7  773c310 added hello.py
8  7275078 More verbosity for the README
9  4f3ea33 Added README

Protips:

1 # set commit message without opening an editor
2 git commit -m 'commit message'
3 # add only parts of what was changed
4 git add -p

Presenter Notes

see what changed

  • use 'git diff'
  • by default diffs current working dir with most recent commit
  • super useful, check out 'git diff --help'

Presenter Notes

 1 $ vim hello.py
 2 $ git diff
 3  diff --git a/hello.py b/hello.py
 4  index 3148dce..33f348b 100644
 5  --- a/hello.py
 6  +++ b/hello.py
 7  @@ -1,3 +1,3 @@
 8   #!/usr/bin/env python
 9 
10  -print "hello world"
11  +print "Hello, world!"
12 $ git commit -am "Fixed up grammar in hello.py"
13 [master 01ee7f3] Fixed up grammar in hello.py
14  1 files changed, 1 insertions(+), 1 deletions(-)

Presenter Notes

check it out

 1 $ git log --oneline --decorate
 2     1c8ce74 (HEAD, origin/newgitseminar, newgitseminar) Added more on branching
 3     4afbe50 Added slides for branching
 4     151dd2f Finished converting to vanilla markdown
 5     5e130da Added git seminar to front page
 6     d219f44 New Git seminar via Landslide now working
 7     dfbc19c (origin/master, origin/HEAD, master) Changed length of excerpt
 8 
 9     # specify a branch that is on the remote named 'origin'
10 $ git checkout origin/master
11     # specify a branch name
12 $ git checkout master
13     # specify commit ID (or hash)
14 $ git dfbc19c
15     # specify a commit that is 5 commits prior to where HEAD is
16 $ git checkout HEAD~5

All those checkouts go to the same commit

check out a specific file

1     # get the older version of file 'filename'
2 $ git checkout -- filename
3     # throw away all local changes and switch to branch 'master'
4 $ git checkout -f master

Presenter Notes

branching

Presenter Notes

branching

  • lightweight (creating a branch requires 40 bytes of space)
  • pointers to different commits
  • conveneint ways to separate tasks and features

Presenter Notes

feature branches

  • use master and develop branches

  • merge features to develop, then to master

  • merge hotfixes directly to master as needed

Presenter Notes

starting with branches

get a branch

1 $ git checkout branchname

create a new branch with name branchname

1 $ git checkout -b branchname
2 #or
3 git branch branchname
4 git checkout branchname

Presenter Notes

develop asynchronously

*   5a376b1 - Merge branch 'master' of github.com:boto/boto
|\
| * 2f03097 - Adding ref/dynamodb to hidden TOC.
| *   649bad2 - Merging in @rdodev's DynamoDB tutorial and adapting it for Layer2
| |\
| | * d5defb8 - Little changes
| | * 0a5046c - Starting point. We can add more details later on.
| | * a99fb2e - Grammar fix
| | * d5d3edb - Minor redaction edits
| | *   e5a397f - Merge branch 'master' of git@github.com:rdodev/boto.git
| | |\
| | | * 8c19b40 - Fixing code example
| | * | f35a84d - Added subsections

merge branches whenever

Presenter Notes

working with branches

push one of your branches up to a remote

1 $ git push origin testfeature:experimental

to display all local branches

1 $ git branch
2       master
3     * develop
4       feature/newMenuBar
5       hotfix/MVP-449

Presenter Notes

bring it back together

 1 git checkout master
 2 git merge experiment
 3 < resolve conflicts >
 4 
 5 Hello
 6 <<<<<<< HEAD
 7 there
 8 =======
 9 experimental
10 >>>>>>> experiment
11 world
12 
13 git commit -m "Merged in my experimental feature"

Presenter Notes

workflows

Presenter Notes

workflows

  • git-flow: we'll get there in a second
  • github-style: a unique take on git-flow, with continuous integration in mind
  • integration manager: one 'integration manager' is the only person with commit access to a 'blessed' repo
  • dictator + lieutenants: great for massive projects (cough-linuxkernel-cough) where people are given responsibility for a subsystem ('lieutenants') and passes merged changes to the dictator to be finally commited

Presenter Notes

git-flow

my personal favorite

Presenter Notes

git is flexible
mix and match workflows

Presenter Notes

Cheat Sheet

 1 # unstage a file
 2 $ git reset HEAD <filename>...
 3 # undo the changes to a file
 4 $ git checkout -- <filename>
 5 # delete a file from your staging area, but not working directory
 6 $ git rm --cached <filename>
 7 # view all unstaged changes
 8 $ git diff
 9 # view all staged changes
10 $ git diff --cached
11 # add a file to your last commit
12 $ git commit -m "A commit"
13 $ git add file_i_forgot.txt
14 $ git commit --amend
15 # see the last 5 days of activity on the repository, concisely
16 $ git log --since=5.days --oneline

Presenter Notes

github.com

Presenter Notes

find a project

Presenter Notes

800,000 original projects

1 >> Repository.count(:conditions => 
2     { :parent_id => nil, :public => 1 })
3 => 805411

Presenter Notes

be part of a community

Presenter Notes

contribute back

  • fork
  • commit
  • push
  • (pull request)

Presenter Notes

get the code

 1 $ git clone git://github.com/some1/project
 2 Cloning into project...
 3 $ cd project/
 4 $ vim README
 5 $ git commit -am 'made it better'
 6 [master dbeb245] made it better
 7  1 files changed, 2 insertions(+), 0 deletions(-)
 8 $ (fork it on github)
 9 $ git remote add myfork git@github.com:you/project.git
10 $ git push myfork master:feature_name
11 ...
12 To git@github.com:you/project.git
13    9457e38..dbeb245  master -> feature_name

improve someone else's repo, in just one minute

Presenter Notes

???

Presenter Notes

Ryan Brown

Programmer, Git User

ryansb@csh.rit.edu

github.com/ryansb

gplus.to/ryansb

Presenter Notes