Your SlideShare is downloading. ×
Git! (a) Gentle InTroduction Bruno Bossola
Git! <ul><li>About version control
Concepts
Working with repos and files
Working with remotes
Branching
Remote branches
Tools
Q&A </li></ul><ul><ul><li>Disclaimer: this is limited   by my own experience:) </li></ul></ul>
About version control Picture courtesy of globalnerdy.com All rights kindly reserved
Version control: centralized Picture courtesy of progit.org. All rights kindly reserved <ul><li>CVS
SVN </li></ul>
Version control: distributed <ul><li>Git
Mercurial
Bazaar </li></ul>Picture courtesy of progit.org. All rights kindly reserved
Concepts
Concepts <ul><li>Snapshots, not deltas
Nearly every operation is local
Integrity is a priority
The “three states </li></ul>
Snapshot, not deltas <ul><li>Deltas are maintained </li><ul><li>CVS, SVN, Bazaar </li></ul></ul>Picture courtesy of progit...
Snapshot, not deltas <ul><li>Full file is mantained </li><ul><li>Git , BitKeeper </li></ul></ul>Picture courtesy of progit...
Most operations are local <ul><li>Your local database contains a full copy of the remote(s)
Browsing, changes, search all happening locally
You can do almost everything  without  network
...and all the db is in a nice, clean , separate .git folder :) </li></ul>
Integrity is a priority <ul><li>Everything in Git is check-summed </li><ul><li>SHA-1 hash
40-character string such as: </li></ul><li>It's impossible to make a change withoug Git knowing it!
Git generally only adds data </li></ul>95b87297210672b16bb70ded20626c9c551ccd58
The Three States <ul><li>Modified
Committed
Staged </li></ul>Picture courtesy of progit.org. All rights kindly reserved
Git Setup  (1) <ul><li>Install git </li></ul><ul><li>apt-get install git-core
yum install git-core
http://code.google.com/p/git-osx-installer
http://code.google.com/p/msysgit </li></ul>
Git Setup  (2) <ul><li>Set your identity
Double check! </li></ul><ul>> git config --global user.name &quot;Mark Joyce&quot; > git config --global user.email mj@xyz...
Working with repos and files Picture courtesy of questionhub.com. All rights kindly reserved
Create a repository <ul><li>Move into an empty folder... </li><ul><li>Creates an empty repository
.git folder contains the database </li></ul></ul><ul>> git init </ul>
Clone a repository <ul><li>Move into an empty folder... </li><ul><li>Different protocols are available </li><ul><li>git (n...
http(s)
ssh </li></ul></ul><li>It's called “clone” because you get a full copy of the repository! </li></ul><ul>> git clone <url> ...
File status  Picture courtesy of progit.org. All rights kindly reserved
Initial status <ul><li>Right after a clone: </li></ul><ul>> git status # On branch master nothing to commit (working direc...
Add a file <ul><li>The new file is now untracked </li></ul><ul>> vi readme.txt > git status # On branch master # Untracked...
Track the file <ul><li>The new file is now tracked and staged </li></ul><ul>>  git add readme.txt > git status # On branch...
Change existing file <ul>> vi annotation.txt > git status # On branch master # Changes to be committed: #  (use &quot;git ...
Track the changed file <ul><li>The changed file is now staged </li></ul><ul>>  git add annotation.txt > git status # On br...
Change already staged file <ul>> vi annotation.txt > git status # On branch master # Changes to be committed: #  (use &quo...
WTF? <ul><li>We have two version of the same file
If you commit now the first one will go in
to stage the existing version: </li></ul><ul>>  git add annotation.txt > git status # On branch master # Changes to be com...
Committing <ul><li>A commit will put files in the staging into the repo
This will invoke your editor, better do this: </li></ul><ul>> git commit </ul><ul>> git commit -m '[GTN-44] scheduler fix'...
Remove existing file  (1) <ul><li>The removal is detected </li></ul><ul>> rm annotation.txt  > git status # On branch mast...
Remove existing file  (2) <ul><li>The removal is staged: if you commit it will be removed from the repo </li></ul><ul>>  g...
From staged to modified  <ul>>  git reset HEAD annotation.txt Unstaged changes after reset: M annotation.txt > git status ...
Discarding changes <ul><li>File recovered!
Yes, “checkout” is a bit weird :) </li></ul><ul>>  git checkout annotation.txt > ls annotation.txt  readme.txt > git statu...
Changing a commit <ul><li>Takes staging area and uses to amend the previous commit </li></ul><ul>>  git commit –amend -m '...
Looking into commits <ul>>  git log commit 57937924f8423d79b5cc50d4fea807b271dc0383 Author: Bruno Bossola <bbossola@gmail....
Looking into one commit <ul><li>...this is reeallly useful! </li></ul><ul>> git show --pretty=&quot;format:&quot; --name-o...
Working with remotes Picture courtesy of useit.com. All rights kindly reserved
Working with remotes <ul><li>You can have multiple remote repos </li><ul><li>usually one, “origin”
sometimes one main r/w, other r/o,
rarely multiple </li></ul><li>Collaborating means  pushing  and  pulling  data from the remote repos </li></ul>
Branching Picture courtesy of campus.houghton.edu. All rights kindly reserved
How Git stores data <ul><li>Git has an internal object database
Upcoming SlideShare
Loading in...5
×

Git - (a) Gentle InTroduction

2,610

Published on

Git speech at Jug Torino november 2010 meeting

Published in: Technology
1 Comment
15 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,610
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
1
Likes
15
Embeds 0
No embeds

No notes for slide

Transcript of "Git - (a) Gentle InTroduction"

  1. 1. Git! (a) Gentle InTroduction Bruno Bossola
  2. 2. Git! <ul><li>About version control
  3. 3. Concepts
  4. 4. Working with repos and files
  5. 5. Working with remotes
  6. 6. Branching
  7. 7. Remote branches
  8. 8. Tools
  9. 9. Q&A </li></ul><ul><ul><li>Disclaimer: this is limited by my own experience:) </li></ul></ul>
  10. 10. About version control Picture courtesy of globalnerdy.com All rights kindly reserved
  11. 11. Version control: centralized Picture courtesy of progit.org. All rights kindly reserved <ul><li>CVS
  12. 12. SVN </li></ul>
  13. 13. Version control: distributed <ul><li>Git
  14. 14. Mercurial
  15. 15. Bazaar </li></ul>Picture courtesy of progit.org. All rights kindly reserved
  16. 16. Concepts
  17. 17. Concepts <ul><li>Snapshots, not deltas
  18. 18. Nearly every operation is local
  19. 19. Integrity is a priority
  20. 20. The “three states </li></ul>
  21. 21. Snapshot, not deltas <ul><li>Deltas are maintained </li><ul><li>CVS, SVN, Bazaar </li></ul></ul>Picture courtesy of progit.org. All rights kindly reserved
  22. 22. Snapshot, not deltas <ul><li>Full file is mantained </li><ul><li>Git , BitKeeper </li></ul></ul>Picture courtesy of progit.org. All rights kindly reserved
  23. 23. Most operations are local <ul><li>Your local database contains a full copy of the remote(s)
  24. 24. Browsing, changes, search all happening locally
  25. 25. You can do almost everything without network
  26. 26. ...and all the db is in a nice, clean , separate .git folder :) </li></ul>
  27. 27. Integrity is a priority <ul><li>Everything in Git is check-summed </li><ul><li>SHA-1 hash
  28. 28. 40-character string such as: </li></ul><li>It's impossible to make a change withoug Git knowing it!
  29. 29. Git generally only adds data </li></ul>95b87297210672b16bb70ded20626c9c551ccd58
  30. 30. The Three States <ul><li>Modified
  31. 31. Committed
  32. 32. Staged </li></ul>Picture courtesy of progit.org. All rights kindly reserved
  33. 33. Git Setup (1) <ul><li>Install git </li></ul><ul><li>apt-get install git-core
  34. 34. yum install git-core
  35. 35. http://code.google.com/p/git-osx-installer
  36. 36. http://code.google.com/p/msysgit </li></ul>
  37. 37. Git Setup (2) <ul><li>Set your identity
  38. 38. Double check! </li></ul><ul>> git config --global user.name &quot;Mark Joyce&quot; > git config --global user.email mj@xyz.com </ul><ul>> git config --list –-global user.name=Mark Joyce user.email=mj@xyz.com http.sslverify=false </ul><ul>> git config --global user.name &quot;Mark Joyce&quot; > git config --global user.email mj@xyz.com </ul>
  39. 39. Working with repos and files Picture courtesy of questionhub.com. All rights kindly reserved
  40. 40. Create a repository <ul><li>Move into an empty folder... </li><ul><li>Creates an empty repository
  41. 41. .git folder contains the database </li></ul></ul><ul>> git init </ul>
  42. 42. Clone a repository <ul><li>Move into an empty folder... </li><ul><li>Different protocols are available </li><ul><li>git (native)
  43. 43. http(s)
  44. 44. ssh </li></ul></ul><li>It's called “clone” because you get a full copy of the repository! </li></ul><ul>> git clone <url> </ul>
  45. 45. File status Picture courtesy of progit.org. All rights kindly reserved
  46. 46. Initial status <ul><li>Right after a clone: </li></ul><ul>> git status # On branch master nothing to commit (working directory clean) </ul>
  47. 47. Add a file <ul><li>The new file is now untracked </li></ul><ul>> vi readme.txt > git status # On branch master # Untracked files: # (use &quot;git add <file>...&quot; to include in what will be committed) # # readme.txt nothing added to commit but untracked files present (use &quot;git add&quot; to track) </ul>
  48. 48. Track the file <ul><li>The new file is now tracked and staged </li></ul><ul>> git add readme.txt > git status # On branch master # Changes to be committed: # (use &quot;git reset HEAD <file>...&quot; to unstage) # # new file: readme.txt # </ul>
  49. 49. Change existing file <ul>> vi annotation.txt > git status # On branch master # Changes to be committed: # (use &quot;git reset HEAD <file>...&quot; to unstage) # # new file: readme.txt # # Changed but not updated: # (use &quot;git add <file>...&quot; to update what will be committed) # (use &quot;git checkout -- <file>...&quot; to discard changes in working directory) # # modified: annotation.txt </ul>
  50. 50. Track the changed file <ul><li>The changed file is now staged </li></ul><ul>> git add annotation.txt > git status # On branch master # Changes to be committed: # (use &quot;git reset HEAD <file>...&quot; to unstage) # # modified: annotation.txt # new file: readme.txt # </ul>
  51. 51. Change already staged file <ul>> vi annotation.txt > git status # On branch master # Changes to be committed: # (use &quot;git reset HEAD <file>...&quot; to unstage) # # new file: readme.txt # # Changed but not updated: # (use &quot;git add <file>...&quot; to update […]) # (use &quot;git checkout -- <file>...&quot; to […]) # # modified: annotation.txt # </ul>
  52. 52. WTF? <ul><li>We have two version of the same file
  53. 53. If you commit now the first one will go in
  54. 54. to stage the existing version: </li></ul><ul>> git add annotation.txt > git status # On branch master # Changes to be committed: # (use &quot;git reset HEAD <file>...&quot; to unstage) # # modified: annotation.txt # new file: readme.txt </ul>
  55. 55. Committing <ul><li>A commit will put files in the staging into the repo
  56. 56. This will invoke your editor, better do this: </li></ul><ul>> git commit </ul><ul>> git commit -m '[GTN-44] scheduler fix' [master 019a938] [GTN-44] scheduler fix 2 files changed, 5 instions(+), 1 dltions(-) create mode 100644 readme.txt </ul>
  57. 57. Remove existing file (1) <ul><li>The removal is detected </li></ul><ul>> rm annotation.txt > git status # On branch master # Changed but not updated: # (use &quot;git add/rm <file>...&quot;) to ...) # (use &quot;git checkout -- <file>...&quot;) to ...) # # deleted: annotation.txt # no changes added to commit (use &quot;git add&quot; and/or &quot;git commit -a&quot;) </ul>
  58. 58. Remove existing file (2) <ul><li>The removal is staged: if you commit it will be removed from the repo </li></ul><ul>> git rm annotation.txt rm 'annotation.txt' > git status # On branch master # Changes to be committed: # (use &quot;git reset HEAD <file>...&quot; to unstage) # # deleted: annotation.txt # </ul>
  59. 59. From staged to modified <ul>> git reset HEAD annotation.txt Unstaged changes after reset: M annotation.txt > git status # On branch master # Changed but not updated: # (use &quot;git add/rm <file>...&quot; to upd...) # (use &quot;git checkout -- <file>...&quot; to dis...) # # deleted: annotation.txt # no changes added to commit (use...) </ul>
  60. 60. Discarding changes <ul><li>File recovered!
  61. 61. Yes, “checkout” is a bit weird :) </li></ul><ul>> git checkout annotation.txt > ls annotation.txt readme.txt > git status # On branch master nothing to commit (working directory clean) </ul>
  62. 62. Changing a commit <ul><li>Takes staging area and uses to amend the previous commit </li></ul><ul>> git commit –amend -m 'oops!' [master 5793792] oops! 2 files changed, 6 insertions(+), 1 deletions(-) create mode 100644 readme.txt </ul>
  63. 63. Looking into commits <ul>> git log commit 57937924f8423d79b5cc50d4fea807b271dc0383 Author: Bruno Bossola <bbossola@gmail.com> Date: Tue Nov 2 23:26:39 2010 +0000 oops! commit 8aefe45413517f71ceb64783cfae58e20031af52 Author: Bruno Bossola <bbossola@gmail.com> Date: Tue Nov 2 22:35:00 2010 +0000 annotations fixed [...] </ul>
  64. 64. Looking into one commit <ul><li>...this is reeallly useful! </li></ul><ul>> git show --pretty=&quot;format:&quot; --name-only d13c1a17cc0599d3ed44cfe98ed3a194df048b15 annotation.txt readme.txt </ul>
  65. 65. Working with remotes Picture courtesy of useit.com. All rights kindly reserved
  66. 66. Working with remotes <ul><li>You can have multiple remote repos </li><ul><li>usually one, “origin”
  67. 67. sometimes one main r/w, other r/o,
  68. 68. rarely multiple </li></ul><li>Collaborating means pushing and pulling data from the remote repos </li></ul>
  69. 69. Branching Picture courtesy of campus.houghton.edu. All rights kindly reserved
  70. 70. How Git stores data <ul><li>Git has an internal object database
  71. 71. It contains </li><ul><li>blob (files)
  72. 72. commit
  73. 73. tree
  74. 74. …and other stuff :) </li></ul></ul>
  75. 75. After a commit... Picture courtesy of progit.org. All rights kindly reserved
  76. 76. After three commits... Picture courtesy of progit.org. All rights kindly reserved
  77. 77. A branch is a pointer <ul><li>A branch is simply a movable pointer </li></ul>Picture courtesy of progit.org. All rights kindly reserved
  78. 78. Creating a branch <ul><li>Create a branch: </li></ul>Picture courtesy of progit.org. All rights kindly reserved <ul>> git branch testing </ul>
  79. 79. Head <ul><ul><li>Git knows what branch you’re currently on with a special pointer called HEAD </li></ul></ul>Picture courtesy of progit.org. All rights kindly reserved
  80. 80. Switching to a branch Picture courtesy of progit.org. All rights kindly reserved <ul><ul><li>Git moves HEAD pointer to the branch pointer </li></ul></ul><ul>> git checkout testing Switched to branch 'testing' </ul>
  81. 81. Changing a file (on a branch) Picture courtesy of progit.org. All rights kindly reserved <ul><ul><li>Git keeps following with HEAD the branch pointer </li></ul></ul><ul>> vi readme.txt > git commit -a -m 'readme file updated' </ul>
  82. 82. Switch to master Picture courtesy of progit.org. All rights kindly reserved <ul><ul><li>Git moves back HEAD to point to masterpointer </li></ul></ul><ul>> git checkout master </ul>
  83. 83. And change again! Picture courtesy of progit.org. All rights kindly reserved <ul><ul><li>Git still keeping separate pointers to the branches </li></ul></ul><ul>> vi readme.txt > git commit -a -m 'readme file now rocks!' </ul>
  84. 84. Now... merge! Picture courtesy of progit.org. All rights kindly reserved <ul><ul><li>A new “merge” commit is generated </li></ul></ul><ul>> git merge testing Merge made by recursive. readme.txt | 1 + 1 files changed, 1 instions(+), 0 dltions(-) </ul>
  85. 85. The Stash <ul><li>Temporary storage area to store changes
  86. 86. you don't want to commit “half-done” and you want to change branch
  87. 87. Very useful for “dirty” works :) </li><ul><li>Usage: git stash push/pop/clean </li></ul></ul>
  88. 88. Remote branches Picture courtesy of maps.google.com. All rights kindly reserved
  89. 89. Remote branches <ul><li>Reference to state of branches on a remote repository
  90. 90. They're local, but cannot be moved
  91. 91. They're moved automatically during synchronization
  92. 92. <remote>/<branch name>
  93. 93. Your default remote is “origin” </li></ul>
  94. 94. Initial clone Picture courtesy of progit.org. All rights kindly reserved
  95. 95. How do I sync? <ul><li>“master” is tracked automatically
  96. 96. “fetch” command will download all the updates from the remote db </li><ul><li>“merge” to merge the branches
  97. 97. “rebase” (let's see this later) </li></ul><li>“pull” is a shortcut for fetch + pull </li></ul><ul>> git pull origin master * branch master -> FETCH_HEAD Already up-to-date. </ul>
  98. 98. I do some work... Picture courtesy of progit.org. All rights kindly reserved
  99. 99. Someone else pushes! Picture courtesy of progit.org. All rights kindly reserved
  100. 100. Synchronize (with fetch) Picture courtesy of progit.org. All rights kindly reserved
  101. 101. And now? <ul><li>Fetch is just fetching all the data, nothing changes
  102. 102. To update your master copy to the remote you may: </li><ul><li>Merge
  103. 103. Rebase </li></ul><li>You may have also “pulled” before (fetch + merge) </li></ul>
  104. 104. Rebasing (the “cool” thing!) <ul><li>Takes all the changes committed on one branch and replay them on another one
  105. 105. No differences in result
  106. 106. Much cleaner history
  107. 107. Branches are then easy to integrate to the master </li></ul>
  108. 108. Rebasing (1) <ul><li>Two branches, diverging, we need to merge </li></ul>Picture courtesy of progit.org. All rights kindly reserved
  109. 109. Rebasing (2) <ul><li>Traditional, using merge </li></ul>Picture courtesy of progit.org. All rights kindly reserved
  110. 110. Rebasing (3) <ul><li>Rock'n roll! </li></ul>Picture courtesy of progit.org. All rights kindly reserved <ul>> git checkout experiment > git rebase master First, rewinding head to replay your work on top of it... Applying: added staged command </ul>
  111. 111. Push <ul><li>The push command is used to send your changes to a remote repo
  112. 112. Anyone who fetch will get the new branch </li></ul><ul>> git push origin experiment Counting objects: 20, done. Compressing objects: 100% (14/14), done. Writing objcts: 100% (15/15), 1.7 KiB, done. Total 15 (delta 5), reused 0 (delta 0) To bbossola@gitent-scm.com... * [new branch] experiment -> experiment </ul>
  113. 113. Getting a remote branch <ul><li>To start contributing on a branch you have to check it out
  114. 114. This branch is now tracked (see next) </li></ul><ul>> git checkout -b serverfix origin/serverfix Branch serverfix set up to track remote branch refs/remotes/origin/serverfix. Switched to a new branch &quot;serverfix&quot; </ul>
  115. 115. Tracking branches <ul><li>Checking out a local branch from a remote branch automatically creates a tracking branch .
  116. 116. Tracking branches are local branches that have a direct relationship to a remote branch
  117. 117. Push/pull will send/fetch+merge contents from the remote automatically </li></ul>
  118. 118. Remote branches <ul><li>Reference to state of branches on a remote repository
  119. 119. They're local, but cannot be moved
  120. 120. They're moved automatically during synchronization
  121. 121. <remote>/<branch name>
  122. 122. Your default remote is “origin” </li></ul>
  123. 123. Tools
  124. 124. CLI tools <ul><li>Command line works like a charm!
  125. 125. You can still revert to your IDE/editor/tool to merge </li><ul><li>...but built-in merge is brilliant! </li></ul><li>To visualize branches: gitk </li><ul><li>(gitx on osx) </li></ul></ul>
  126. 126. IDE support <ul><li>Eclipse: poor, sloppy </li><ul><li>The guys of Egit wants to develop it “pure”, so it'll take years :) </li></ul><li>Idea: good support
  127. 127. NetBeans: experimental (buggy) support
  128. 128. ...yeah, use the command line! </li></ul>
  129. 129. Server side solutions <ul><li>GitHub
  130. 130. BitBucket
  131. 131. GitEnterprise (shameless plug!) </li></ul>
  132. 132. Links <ul><li>ProGit book </li><ul><li>http://progit.org/book/ </li></ul><li>Community Git book </li><ul><li>http://book.git-scm.com/ </li></ul><li>GitEnterprise </li><ul><li>http://www.gitenterprise.com </li></ul></ul>
  133. 133. Q&A Ahhh.... a free artwork, finally!

×