3. Who am I?
• For the people in the room:
• You know me
• Everyone else:
• Chris Miller
• Sr. Developer -- Huffington Post
• Twitter: @ee99ee
• Email: chris.miller@huffingtonpost.com
4. Git? Git Wha?
• When the office git expert has to come fix everything
5. Git? Git Wha?
• When the office git expert has to come fix everything
8. Git Background
• Distributed revision control system
• Developed by Linus Torvalds for Linux kernel development
9. Git Background
• Distributed revision control system
• Developed by Linus Torvalds for Linux kernel development
• Designed for distributed development
10. Git Background
• Distributed revision control system
• Developed by Linus Torvalds for Linux kernel development
• Designed for distributed development
• Designed for large projects (large codebase)
17. First Steps
• Download and install git client (http://git-scm.com/downloads)
18. First Steps
• Download and install git client (http://git-scm.com/downloads)
• Create the repo, add your files, and commit
19. First Steps
• Download and install git client (http://git-scm.com/downloads)
• Create the repo, add your files, and commit
• Initializeinit repo:
git
the
20. First Steps
• Download and install git client (http://git-scm.com/downloads)
• Create the repo, add your files, and commit
• Initializeinit repo:
git
the
• Addgit add myfile.c
your file(s):
21. First Steps
• Download and install git client (http://git-scm.com/downloads)
• Create the repo, add your files, and commit
• Initializeinit repo:
git
the
• Addgit add myfile.c
your file(s):
• Commit!:
git commit -m “I haz filez! yay!”
26. The Basics
• Git repositories are local
• Repositories are relative to your working environment (not the server)
27. The Basics
• Git repositories are local
• Repositories are relative to your working environment (not the server)
• A commit goes to your local repository -- not some server (as in SVN)
28. The Basics
• Git repositories are local
• Repositories are relative to your working environment (not the server)
• A commit goes to your local repository -- not some server (as in SVN)
• A commit is a snapshot -- A point in time
29. The Basics
• Git repositories are local
• Repositories are relative to your working environment (not the server)
• A commit goes to your local repository -- not some server (as in SVN)
• A commit is a snapshot -- A point in time
• Commits are metadata about the commit before and changes now
30. Cloning
• To get a remote repo, you need the URL
• git clone [url]
31. The Index
• The index tracks what you want to commit
• Files are in one of three states
• Tracked
• Ignored
• Untracked
33. Staging and Committing
• Untracked files are “staged” prior to a commit
[~/domains/gittalk]$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
#
test.c
nothing added to commit but untracked files present (use "git add" to track)
34. Staging and Committing
• Untracked files are “staged” prior to a commit
[~/domains/gittalk]$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
#
test.c
nothing added to commit but untracked files present (use "git add" to track)
[~/domains/gittalk]$ git add test.c
35. Staging and Committing
• Untracked files are “staged” prior to a commit
[~/domains/gittalk]$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
#
test.c
nothing added to commit but untracked files present (use "git add" to track)
[~/domains/gittalk]$ git add test.c
[~/domains/gittalk]$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
#
new file: test.c
#
36. Staging and Committing
• Untracked files are “staged” prior to a commit
[~/domains/gittalk]$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
#
test.c
nothing added to commit but untracked files present (use "git add" to track)
[~/domains/gittalk]$ git add test.c
[~/domains/gittalk]$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
#
new file: test.c
#
[~/domains/gittalk]$ git commit -m “I haz moar codez”
[master (root-commit) 3e44a84] I haz moar codez
1 file changed, 1 insertion(+)
create mode 100644 test.c
37. Removing and Moving
• Removing a file from the file system is not enough.
• You must tell git to remove from the repo as well (and commit that change)
38. Removing and Moving
• Removing a file from the file system is not enough.
• You must tell git to remove from the repo as well (and commit that change)
[~/domains/gittalk]$ git rm test.c
rm 'test.c'
[~/domains/gittalk]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
#
deleted: test.c
#
[~/domains/gittalk]$ git commit -m "I removed a file"
[master e993a90] I removed a file
1 file changed, 1 deletion(-)
delete mode 100644 test.c
39. Removing and Moving
• Removing a file from the file system is not enough.
• You must tell git to remove from the repo as well (and commit that change)
[~/domains/gittalk]$ git rm test.c
rm 'test.c'
[~/domains/gittalk]$ git status
# On branch master
# Changes to be committed:
• P. S. - Moving files works the same way
# (use "git reset HEAD <file>..." to unstage)
#
#
deleted: test.c
#
• Use git mv in the same sense
[~/domains/gittalk]$ git commit -m "I removed a file"
[master e993a90] I removed a file
1 file changed, 1 deletion(-)
delete mode 100644 test.c
40. Ignoring Stuff
• Tell git to ignore files with .gitignore
• Text file located in root of git repo
• Tracked and committed like a normal file
41. Ignoring Stuff /tmp/
*~
*.lock
*.DS_Store
• Tell git to ignore files with .gitignore *.swp
*.out
~$*
• Text file located in root of git repo [Tt]humbs.db
/.svn/*
*/.svn/*
• Tracked and committed like a normal file /CVS/*
*/CVS/*
.cvsignore
*/.cvsignore
.DS_Store
Example from Huffington Post .gitignore: Icon?
._*
.Spotlight-V100
.Trashes
Desktop.ini
*.hg
*.project
42. Peeking Inside
• A single .git directory in root of project structure contains git meta data
• Types of meta data:
• Blobs
• Trees
• Commits
• Tags
43. Peeking Inside
• Blobs (Binary Large Objects) are copies of the actual files
• A SHA1 hash of each file is made and the contents stored as a
filename of the hash. Example:
• .git/objects/9da581d910c9c4ac93557ca4859e767f5caf5169
44. Peeking Inside
• Blobs (Binary Large Objects) are copies of the actual files
• A SHA1 hash of each file is made and the contents stored as a
filename of the hash. Example:
• .git/objects/9da581d910c9c4ac93557ca4859e767f5caf5169
• Each file stored only once
• Identical files, regardless of directory, stored only once
• SHA1 hash is identical on everyone’s machine
• Same file always computes to the same hash
46. Peeking Inside
• Tree: Mapping of directory path, blob id, and meta info (chmod, etc.)
47. Peeking Inside
• Tree: Mapping of directory path, blob id, and meta info (chmod, etc.)
• Commit
• Meta data about author, comment, and tree reference
• Allows git to compile a complete snapshot of the tree at that given
time
48. Peeking Inside
• Tree: Mapping of directory path, blob id, and meta info (chmod, etc.)
• Commit
• Meta data about author, comment, and tree reference
• Allows git to compile a complete snapshot of the tree at that given
time
• Tags: Arbitrary, user-defined pointer to a given commit. Example:
• Ver2.0-Alpha -> 9da581d910c9c4ac93557ca4859e767f5caf5169
49. The Index and Committing
• The index is a list of files and their blob references for the current
state of the repo
[~/domains/HuffingtonPost/huffpost-web]$ git ls-files -s
100644 197687b4afab0816d33dcdfd68e462880bdac997 0
www/public/twitter/public_info.php
100644 0b5a713e78d33b211e334a5fe2df0d7d66eac3cb 0
www/public/twitter/quicktwitter_suggestion.php
100644 d1ddd421d284e167079124cef22b9dd788952036 0
www/public/twitter/track_twitter_suggestions.php
100644 89f1197c8d71064fbb4a4add6310b314c073f3cd 0
www/public/twitter/twitter_suggestions.php
100644 7f57b7ac131aed04832034dc76c59ab30a92f020 0
www/public/typekey/regkey.txt
100644 3a4bec59ae14a44834ef6dfeb1cc26a19a62d24d 0
www/public/uk-celebrity/includes/modules/ukCeleb_mod_Dl.inc.php
100644 32e8b6a04ae8284288133764e62f7a067b63cf08 0
www/public/uk-celebrity/includes/modules/ukCeleb_mod_FeaturedBlogs.inc.php
100644 89e0386b2dffcf0f105280017d27367269a899d0 0
www/public/uk-celebrity/includes/modules/ukCeleb_mod_Photos.inc.php
100644 91db4907100cd15728de94e5951900d2cdc91330 0
www/public/uk-celebrity/includes/modules/
ukCeleb_mod_RRailModules.inc.php
(....)
50. The Index and Committing
• The index is a list of files and their blob references for the current
state of the repo
[~/domains/HuffingtonPost/huffpost-web]$ git ls-files -s
100644 197687b4afab0816d33dcdfd68e462880bdac997 0
www/public/twitter/public_info.php
100644 0b5a713e78d33b211e334a5fe2df0d7d66eac3cb 0
www/public/twitter/quicktwitter_suggestion.php
100644 d1ddd421d284e167079124cef22b9dd788952036 0
www/public/twitter/track_twitter_suggestions.php
100644 89f1197c8d71064fbb4a4add6310b314c073f3cd 0
www/public/twitter/twitter_suggestions.php
100644 7f57b7ac131aed04832034dc76c59ab30a92f020 0
www/public/typekey/regkey.txt
100644 3a4bec59ae14a44834ef6dfeb1cc26a19a62d24d 0
www/public/uk-celebrity/includes/modules/ukCeleb_mod_Dl.inc.php
100644 32e8b6a04ae8284288133764e62f7a067b63cf08 0
www/public/uk-celebrity/includes/modules/ukCeleb_mod_FeaturedBlogs.inc.php
100644 89e0386b2dffcf0f105280017d27367269a899d0 0
www/public/uk-celebrity/includes/modules/ukCeleb_mod_Photos.inc.php
100644 91db4907100cd15728de94e5951900d2cdc91330 0
www/public/uk-celebrity/includes/modules/
ukCeleb_mod_RRailModules.inc.php
(....)
• Tip: To keep things brief, sometimes only the prefix is used. To lookup
the full commit id:
[~/domains/HuffingtonPost/huffpost-web]$ git rev-parse 3b18e512d
3b18e512dba79e4c8300dd08aeb37f8e728b8dad
51. Committing: One more thing
• Commit often
• When in doubt, commit
• Commit often -- no, really
52. Branching
• Fundamental way to create a separate line of development
• Segment development of multiple features/bug fixes concurrently
without one affecting the other
• When in doubt, branch
53. Branching
• Fundamental way to create a separate line of development
• Segment development of multiple features/bug fixes concurrently
without one affecting the other
• When in doubt, branch
• Tip: I create a branch for every JIRA ticket and name it the ticket ID
55. Branching
• Create a new branch:
[~/domains/gittalk]$ git checkout -b MyFirstBranch
Switched to a new branch 'MyFirstBranch'
56. Branching
• Create a new branch:
[~/domains/gittalk]$ git checkout -b MyFirstBranch
Switched to a new branch 'MyFirstBranch'
• See what branch you are on:
[~/domains/gittalk]$ git branch
* MyFirstBranch
master
57. Branching
• Create a new branch:
[~/domains/gittalk]$ git checkout -b MyFirstBranch
Switched to a new branch 'MyFirstBranch'
• See what branch you are on:
[~/domains/gittalk]$ git branch
* MyFirstBranch
master
• Switch branches
[~/domains/gittalk]$ git checkout master
Switched to branch 'master'
[~/domains/gittalk]$ git branch
MyFirstBranch
* master
59. Sharing: I haz codez
• Git has no central repo -- Your working directory is a repo
60. Sharing: I haz codez
• Git has no central repo -- Your working directory is a repo
• Collaboration is done by “pushing” and “pulling” repos
• A push is syncing changes in your repo to a remote repo
• A pull is integrating changes to your repo from a remote repo
61. Collaborating
• When a coworker and I are working on the same branch:
62. Collaborating
• When a coworker and I are working on the same branch:
63. Remote Repos
• Any repo can be defined as a remote repo
• When you “clone” a repo, that repo is automatically added as a
remote repo called “origin”
64. Remote Repos
• Any repo can be defined as a remote repo
• When you “clone” a repo, that repo is automatically added as a
remote repo called “origin”
• You can view remote repos and add more:
[~/huffpost-web]$ git remote add menachem git@github.com:menachemd/huffpost-web.git
[~/huffpost-web]$ git remote -v
origin
git@github.com:ee99ee/huffpost-web.git (fetch)
origin
git@github.com:ee99ee/huffpost-web.git (push)
menachem
git@github.com:menachemd/huffpost-web.git (fetch)
menachem
git@github.com:menachemd/huffpost-web.git (push)
upstream
git@github.com:huffingtonpost/huffpost-web.git (push)
upstream
git@github.com:huffingtonpost/huffpost-web.git (fetch)
69. Pulling
• The git pull command can be used to
do git fetch + git merge automagically
• I like to see what is going to merge
before it does, so I use git fetch + git
merge
70. Pulling
• Repos can get big... but, there is only one copy of each unique file
71. Pulling
• Repos can get big... but, there is only one copy of each unique file
• How I feel when I fetch big repos:
72. Pulling
• Repos can get big... but, there is only one copy of each unique file
• How I feel when I fetch big repos:
75. Ye Olde Rebase
• Git maintains a history of every commit
• A merge creates a new commit point and adds changes to the end of
the log
76. Ye Olde Rebase
• Git maintains a history of every commit
• A merge creates a new commit point and adds changes to the end of
the log
• Git rebase rewrites the log -- it rewrites history
Use with caution
(but don’t avoid all together)
77. Pushing
• To send code to a remote repo, push:
[~/huffpo-web]$ git push remote_name my_branch
• Remember to commit before pushing
78. Pushing
• To send code to a remote repo, push:
[~/huffpo-web]$ git push remote_name my_branch
• Remember to commit before pushing
When I push before committing:
79. Github
• If using github, you can issue “pull requests” to other users requesting
they integrate your changes
80. Pull Requests
• The owner of the repo to whom you send the pull request can then
merge your changes with theirs
• This merge can be done on github.com if no conflicts exist
• If conflict exists, a git fetch and git merge is necessary (by
them)