Git Basics (Professionals)

1,172 views

Published on

Slides from DevCon Git Code Camp for Professionals, 15 March 2014

http://devcon.ph/events/git-code-camp-for-professionals

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,172
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
23
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Git Basics (Professionals)

  1. 1. Version Control with Git DevCon Git Code Camp for Professionals 15 March 2014 http://devcon.ph/events/git-code-camp-for-professionals
  2. 2. Coverage This workshop will cover the basics of Git and how to integrate Git and Github into your day-to-day work. Slides: https://speakerdeck.com/bryanbibat/git-basics-professionals
  3. 3. Installation Windows: http://msysgit.github.io/ Mac: http://code.google.com/p/git-osx-installer Linux (Debian/Ubuntu): apt-get install git-core gitk Linux (Fedora): yum install git gitk
  4. 4. Verify installation Windows: Open "Git Bash" Mac/Linux: Open your terminal To verify, run: $ git --version
  5. 5. Yes, we are going to use the command line today
  6. 6. Yes, we are going to use the command line today But I'll also demo how you can use GUI clients.
  7. 7. GUI Installation TortoiseGit: https://code.google.com/p/tortoisegit/ Github for Windows: http://windows.github.com/ Github for Mac: http://mac.github.com/ Others: http://git-scm.com/downloads/guis
  8. 8. Yes, we are going to use the command line today Git features not available in some GUI clients are marked !
  9. 9. Initial Setup $ git config --global user.name "Your Name" $ git config --global user.email "your_email@whatever.com" Windows: $ git config --global core.autocrlf true $ git config --global core.safecrlf true Linux/Mac: $ git config --global core.autocrlf input $ git config --global core.safecrlf true
  10. 10. Review...
  11. 11. Version Control
  12. 12. Version Control aka Revision Control
  13. 13. Version Control aka Revision Control aka How we do things in the Real World
  14. 14. Sharing Code
  15. 15. Working as a Team
  16. 16. Version Control
  17. 17. Why Version Control?
  18. 18. Reason #1: "Versioning"
  19. 19. Wait a minute...
  20. 20. Finer-grained Control
  21. 21. Enough talk. Let's begin...
  22. 22. Create your first repository $ mkdir devcon-git101 $ cd devcon-git101 $ git init
  23. 23. Create your first repository $ mkdir devcon-git101 $ cd devcon-git101 $ git init
  24. 24. Create your first repository $ mkdir devcon-git101 $ cd devcon-git101 $ git init Here we create a project folder
  25. 25. Create your first repository $ mkdir devcon-git101 $ cd devcon-git101 $ git init … Then we initialize it as a git repository.
  26. 26. Create your first repository $ mkdir devcon-git101 $ cd devcon-git101 $ git init … Then we initialize it as a git repository. Git can now track the changes inside our project folder.
  27. 27. Create your first commit First create a file "hello.txt" containing: Hello Then run the following commands: $ git add hello.txt $ git commit -m "Initial Commit"
  28. 28. View the repository history $ git log (press q to exit)
  29. 29. View the pretty repo history $ git log --graph --pretty=oneline (press q to exit)
  30. 30. Ah, what the hell... Windows/Linux: $ gitk Mac: $ gitx
  31. 31. Create your second commit Modify "hello.txt" to add "world": Hello World! Then run the following commands: $ git add hello.txt $ git commit -m "Make hello.txt more exciting"
  32. 32. View the updated history Windows/Linux: $ gitk Mac: $ gitx
  33. 33. What just happened?
  34. 34. http://git-scm.com/book/en/Getting-Started-Git-Basics
  35. 35. http://git-scm.com/book/en/Getting-Started-Git-Basics git add git commit
  36. 36. Initial Commit6fba518
  37. 37. Initial Commit6fba518 object name aka object id, commit hash ● SHA-1 hash / checksum for verifying the integrity of the contents of the commit ● Calculated based on file contents and metadata like last updated date i.e. yours will be different
  38. 38. Initial Commit6fba518 Make hello.txt more excitinge642771
  39. 39. Initial Commit6fba518 Make hello.txt more excitinge642771 No, this is not a mistake; commits refer to their parent(s), not the other way around.
  40. 40. Commit multiple files Create a file "names.txt" containing: Alice Bob Cindy
  41. 41. Commit multiple files Create a file "numbers.txt" containing: 3 9 16 12 8.2 4
  42. 42. Commit multiple files Run the following commands: $ git add names.txt numbers.txt $ git commit -m "Create 2 files in a single commit"
  43. 43. Initial Commit6fba518 Make hello.txt more excitinge642771
  44. 44. Initial Commit 1 file created 6fba518 Make hello.txt more exciting 1 file modified e642771 Create 2 files in a single commit 2 files created 7c57165 Each commit deals with a set of files
  45. 45. We've covered "Save", but before we move on to "Load"...
  46. 46. http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository
  47. 47. File Status (all unmodified) $ git status # On branch master nothing to commit, working directory clean
  48. 48. File Status (untracked) Create a file "animals.txt" containing: Dogs Cats Mice
  49. 49. File Status (untracked) $ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # animals.txt nothing added to commit but untracked files present (use "git add" to track)
  50. 50. File Status (untracked and modified) Modify "names.txt" to add "Janet": Alice Bob Janet Cindy
  51. 51. File Status (untracked and modified) $ 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: names.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # animals.txt no changes added to commit (use "git add" and/or "git commit -a")
  52. 52. File Status (untracked and staged) $ git add names.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: names.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # animals.txt !
  53. 53. File Status (commit staged) $ git commit -m "Add Janet" [master 5e545ed] Add Janet 1 file changed, 1 insertion(+) $ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # animals.txt
  54. 54. Shortcuts
  55. 55. Stage a folder Modify "names.txt" to add "Ramon": Alice Ramon Bob Janet Cindy
  56. 56. Stage a folder $ git add . $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: animals.txt # modified: names.txt # !
  57. 57. Unstage a file $ git reset HEAD names.txt Unstaged changes after reset: M names.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: animals.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: names.txt # !
  58. 58. Unmodify a file $ git checkout -- names.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: animals.txt #
  59. 59. do it again... Modify "names.txt" to add "Ramon": Alice Ramon Bob Janet Cindy
  60. 60. Stage and commit EVERYTHING (except untracked files) $ git commit -a -m "Commit unrelated changes.. DON'T DO THIS" [master 61f1cd8] Commit unrelated changes.. DON'T DO THIS 2 files changed, 4 insertions(+) create mode 100644 animals.txt $ git status # On branch master nothing to commit, working directory clean Note: using "-a" will also stage moved and renamed files.
  61. 61. Amend last commit $ git commit -m "Commit unrelated changes... DON'T DO THIS" --amend [master 3a0eac3] Commit unrelated changes... DON'T DO THIS 2 files changed, 4 insertions(+) create mode 100644 animals.txt !
  62. 62. On to "Load"...
  63. 63. Initial Commit6fba518 Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Commit unrelated changes... DON'T DO THIS3a0eac3
  64. 64. Initial Commit6fba518 Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Commit unrelated changes... DON'T DO THIS3a0eac3 HEAD
  65. 65. Create a reverting commit $ git revert HEAD --no-edit [master 2a1b52e] Revert "Commit unrelated changes... DON'T DO THIS" 2 files changed, 4 deletions(-) delete mode 100644 animals.txt
  66. 66. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Commit unrelated changes... DON'T DO THIS3a0eac3 HEAD
  67. 67. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Revert "Commit unrelated changes... DON'T DO THIS" 2a1b52e HEAD Commit unrelated changes... DON'T DO THIS3a0eac3
  68. 68. Move HEAD to a commit discarding changes $ git reset --hard 7c57165 HEAD is now at 7c57165 Create 2 files in a single commit (You can use the first few characters of the object ID instead of the 40 characters)
  69. 69. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Revert "Commit unrelated changes... DON'T DO THIS" 2a1b52e HEAD Commit unrelated changes... DON'T DO THIS3a0eac3
  70. 70. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Revert "Commit unrelated changes... DON'T DO THIS" 2a1b52e HEAD Commit unrelated changes... DON'T DO THIS3a0eac3
  71. 71. View history $ git log --graph --pretty=format:'%h %s%d' --all * 7c57165 Create 2 files in a single commit (HEAD, master) * e642771 Make hello.txt more exciting * 6fba518 Initial Commit or $ gitk --all
  72. 72. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 HEAD
  73. 73. Move HEAD to a commit $ git reset --hard 2a1b52e HEAD is now at 2a1b52e Revert "Commit unrelated changes... DON'T DO THIS" !
  74. 74. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 HEAD
  75. 75. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Revert "Commit unrelated changes... DON'T DO THIS" 2a1b52e HEAD Commit unrelated changes... DON'T DO THIS3a0eac3
  76. 76. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Revert "Commit unrelated changes... DON'T DO THIS" 2a1b52e HEAD Commit unrelated changes... DON'T DO THIS3a0eac3
  77. 77. Unreferenced commits will not show up in the history. Coincidentally, we can use Tags to refer to a commit.
  78. 78. Tagging $ git tag tagging-demo !
  79. 79. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Revert "Commit unrelated changes... DON'T DO THIS" 2a1b52e HEAD Commit unrelated changes... DON'T DO THIS3a0eac3
  80. 80. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Revert "Commit unrelated changes... DON'T DO THIS" 2a1b52e HEAD Commit unrelated changes... DON'T DO THIS3a0eac3 tagging-demo
  81. 81. Going back... $ git reset --hard 7c57165 HEAD is now at 7c57165 Create 2 files in a single commit !
  82. 82. Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 Add Janet5e545ed Revert "Commit unrelated changes... DON'T DO THIS" 2a1b52e HEAD Commit unrelated changes... DON'T DO THIS3a0eac3 tagging-demo
  83. 83. View history $ git log --graph --pretty=format:'%h %s%d' --all * 2a1b52e Revert "Commit unrelated changes... DON'T DO THIS" (tagging-demo) * 3a0eac3 Commit unrelated changes... DON'T DO THIS * 5e545ed Add Janet * 7c57165 Create 2 files in a single commit (HEAD, master) * e642771 Make hello.txt more exciting * 6fba518 Initial Commit or $ gitk --all
  84. 84. Wrapping up, tag current... $ git tag end-part1 !
  85. 85. ...and unreference PDAF $ git tag -d tagging-demo Deleted tag 'tagging-demo' (was 2a1b52e)
  86. 86. Initial Commit6fba518 Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 HEAD end-part1
  87. 87. Best Practices
  88. 88. Write good commit messages You should be able to get an idea what changed and why just by looking at the commit messages
  89. 89. Commit related changes This would make it easier to roll back changes
  90. 90. Commit often
  91. 91. Do not commit generated files E.g. compiled bytecode or executables, log files, temporary files, etc.
  92. 92. Do not commit sensitive information E.g. passwords, settings. You can, however, commit templates.
  93. 93. Helpful Stuff that we will not explain in detail...
  94. 94. .gitignore https://github.com/github/gitignore
  95. 95. git stash
  96. 96. git blame
  97. 97. Summary of Commands git init - initialize repository git add - add files/folders to staging git commit - commit files in staging git status - view status of repository git log / gitk - view history of repository git revert - unstage files git reset - move HEAD to another commit git tag - tag a commit
  98. 98. Summary of Command Variations git commit -a - auto-stage all deleted, moved, modified gitk --all / git log --all - include all branches, tags git tag -d - delete tag
  99. 99. Why Version Control?
  100. 100. Reason #2: Backup
  101. 101. Reason #2: Backup
  102. 102. Reason #2: Collaboration
  103. 103. If we were pressed for time... ● Register at GitHub ● Learn git clone ● Learn git push / git pull ● Learn how to fix merge conflicts
  104. 104. ...but we're not, so let's first discuss something that will help us later on.
  105. 105. Branching
  106. 106. File Status (all unmodified) $ git status # On branch master nothing to commit, working directory clean
  107. 107. File Status (all unmodified) $ git status # On branch master nothing to commit, working directory clean
  108. 108. Initial Commit6fba518 Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 HEAD end-part1
  109. 109. Initial Commit6fba518 Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 master end-part1 HEAD
  110. 110. Initial Commit6fba518 Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 master end-part1 HEAD in other version control systems, master is called trunk
  111. 111. Create a branch $ git branch testing
  112. 112. Initial Commit6fba518 Make hello.txt more excitinge642771 Create 2 files in a single commit7c57165 master end-part1 HEAD
  113. 113. 6fba518 e642771 7c57165 master end-part1 HEAD testing
  114. 114. Switch to branch $ git checkout testing Switched to branch 'testing' $ git status # On branch testing nothing to commit, working directory clean
  115. 115. 6fba518 e642771 7c57165 master end-part1 HEAD testing
  116. 116. 6fba518 e642771 7c57165 master end-part1 HEAD testing
  117. 117. Commit to new branch Modify "names.txt" to add "Eve": Alice Bob Cindy Eve
  118. 118. Commit to new branch $ git commit -am "Add Eve" [testing cdd47c2] Add Eve 1 file changed, 1 insertion(+) $ gitk
  119. 119. 6fba518 e642771 7c57165 master end-part1 HEAD testing
  120. 120. 6fba518 e642771 7c57165 master end-part1 HEAD testing cdd47c2
  121. 121. 6fba518 e642771 7c57165 master end-part1 HEAD testingcdd47c2
  122. 122. Switch back to master $ git checkout master Switched to branch 'master'
  123. 123. 6fba518 e642771 7c57165 master end-part1 HEAD testingcdd47c2
  124. 124. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 HEAD
  125. 125. Difference between log --all vs normal log $ gitk
  126. 126. 6fba518 e642771 7c57165 master end-part1 HEAD
  127. 127. Difference between log --all vs normal log $ gitk --all
  128. 128. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 HEAD
  129. 129. Commit to master Modify "names.txt" to add "Billy": Alice Billy Bob Cindy $ git commit -am "Add Billy" [master cc3044c] Add Billy 1 file changed, 1 insertion(+)
  130. 130. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 HEAD
  131. 131. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 HEAD cc3044c
  132. 132. Shortcut: create and switch $ git checkout -b testing2 Switched to branch 'testing2'
  133. 133. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 HEAD cc3044c
  134. 134. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 cc3044c HEAD testing2
  135. 135. Commit to testing2 Modify "names.txt" to add "Dave": Alice Billy Bob Cindy Dave $ git commit -am "Add Dave" [testing2 80414cf] Add Dave 1 file changed, 1 insertion(+)
  136. 136. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 cc3044c HEAD testing2
  137. 137. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 cc3044c HEADtesting2 80414cf
  138. 138. yay, mukha nang puno
  139. 139. Go back to master $ git checkout master Switched to branch 'master' $ gitk --all
  140. 140. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 cc3044c HEAD testing2 80414cf
  141. 141. Merge another branch $ git merge testing Auto-merging names.txt Merge made by the 'recursive' strategy. names.txt | 1 + 1 file changed, 1 insertion(+)
  142. 142. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 cc3044c HEAD testing2 80414cf
  143. 143. 6fba518 e642771 7c57165 master end-part1 testingcdd47c2 cc3044c HEADtesting2 80414cf f56f4fa Merge branch 'testing'
  144. 144. Merge Conflict $ git merge testing2 Auto-merging names.txt CONFLICT (content): Merge conflict in names.txt Automatic merge failed; fix conflicts and then commit the result.
  145. 145. Merge Conflict Open names.txt: Alice Billy Bob Cindy <<<<<<< HEAD Eve ======= Dave >>>>>>> testing2
  146. 146. Merge Conflict Open names.txt: Alice Billy Bob Cindy <<<<<<< HEAD Eve ======= Dave >>>>>>> testing2 version from HEAD i.e master
  147. 147. Merge Conflict Open names.txt: Alice Billy Bob Cindy <<<<<<< HEAD Eve ======= Dave >>>>>>> testing2 version from testing2
  148. 148. Resolving Merge Conflict Edit names.txt removing the markers: Alice Billy Bob Cindy Dave Eve
  149. 149. Resolving Merge Conflict Commit the resolved merge conflict $ git commit -am "Merge branch 'testing2' and fix conflict" [master 07e83b3] Merge branch 'testing2' and fix conflict
  150. 150. e642771 7c57165 master end-part1 testingcdd47c2 cc3044c HEADtesting2 80414cf f56f4fa Merge branch 'testing'
  151. 151. e642771 7c57165 master end-part1 testingcdd47c2 cc3044c HEAD testing2 80414cf f56f4fa Merge branch 'testing' 07e83b3 Merge branch 'testing2' and fix conflict
  152. 152. Another way to merge: Rebasing
  153. 153. Working on two branches $ git checkout -b merging-demo Switched to a new branch 'merging-demo'
  154. 154. Commit to merging-demo Modify "names.txt" to remove "Alice": Billy Bob Cindy Dave Eve $ git commit -am "Remove Alice" [merging-demo 00b26cb] Remove Alice 1 file changed, 1 deletion(-)
  155. 155. Commit to merging-demo Modify "names.txt" to remove "Cindy": Billy Bob Dave Eve $ git commit -am "Remove Cindy" [merging-demo b115e79] Remove Cindy 1 file changed, 1 deletion(-)
  156. 156. Switch back to master $ git checkout master Switched to branch 'master'
  157. 157. Commit to master Modify "numbers.txt" to remove "8.2": 3 9 16 12 4 $ git commit -am "Remove 8.2" [master 0c1f192] Remove 8.2 1 file changed, 1 deletion(-)
  158. 158. Commit to master Modify "numbers.txt" to remove "9": 3 16 12 4 $ git commit -am "Remove 9" [master bc3583d] Remove 9 1 file changed, 1 deletion(-)
  159. 159. master b115e79 07e83b3 HEAD merging-demo 00b26cb 0c1f192 bc3583d
  160. 160. master b115e79 07e83b3 HEAD merging-demo 00b26cb 0c1f192 bc3583d Merging aff102e
  161. 161. Rebasing $ git rebase merging-demo First, rewinding head to replay your work on top of it... Applying: Remove 8.2 Applying: Remove 9 !
  162. 162. master b115e79 07e83b3 HEAD merging-demo 00b26cb 0c1f192 bc3583d
  163. 163. master b115e79 07e83b3 HEAD merging-demo 00b26cb 0c1f192 bc3583d Remove 8.2 Remove 9
  164. 164. master b115e79 07e83b3 HEAD merging-demo 00b26cb 67b81ce 73dd819 Remove 8.2 Remove 9
  165. 165. master b115e79 07e83b3 HEAD merging-demo 00b26cb 67b81ce 73dd819 Remove 8.2 Remove 9
  166. 166. Later na lang yung merge vs rebase
  167. 167. Malapit na tayo mag-Github, but first...
  168. 168. Remote Repositories
  169. 169. Github for Windows only supports Github
  170. 170. Clone into another folder $ cd .. $ git clone devcon-git101 git101 Cloning into 'git101'... done.
  171. 171. Check the clone repo $ cd git101 $ gitk
  172. 172. merging-demo master HEAD /devcon-git101 merging-demo master HEAD /git101
  173. 173. merging-demo master HEAD /devcon-git101 merging-demo master HEAD /git101 remotes/origin/ merging-demo remotes/origin/ master
  174. 174. Show remote repos $ git remote origin
  175. 175. "origin" remote repo (default repo referring to the repo's origin) $ git remote show origin * remote origin Fetch URL: c:/Users/user/devcon-git101 Push URL: c:/Users/user/devcon-git101 HEAD branch: master Remote branches: master tracked merging-demo tracked testing tracked testing2 tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (up to date)
  176. 176. master HEAD /devcon-git101 master HEAD /git101 remotes/origin/ master
  177. 177. Synchronizing with Remote Repos
  178. 178. Fetching demo Go back to /devcon-git101 (either open a new terminal/Git Bash window) and modify "names.txt" to add Greg: Billy Bob Dave Eve Greg $ git commit -am "Add Greg" [master cf5f902] Add Greg 1 file changed, 1 insertion(+)
  179. 179. master HEAD /devcon-git101 master HEAD /git101 remotes/origin/ master
  180. 180. master HEAD /devcon-git101 master HEAD /git101 remotes/origin/ master
  181. 181. Fetching demo Go back to git101 and fetch the changes: $ cd ../git101 $ git fetch remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From c:/Users/user/devcon-git101 73dd819..cf5f902 master -> origin/master
  182. 182. master HEAD /devcon-git101 master HEAD /git101 remotes/origin/ master
  183. 183. master HEAD /devcon-git101 master HEAD /git101 remotes/origin/ master
  184. 184. Merge the fetched branch $ git merge origin/master Updating 73dd819..cf5f902 Fast-forward names.txt | 1 + 1 file changed, 1 insertion(+)
  185. 185. master HEAD /devcon-git101 master HEAD /git101 remotes/origin/ master
  186. 186. master HEAD /devcon-git101 master HEAD /git101 remotes/origin/ master A fast-forward occurs when you merge a branch which has HEAD as an ancestor. In this case, only the references are affected.
  187. 187. Shortcut $ git fetch $ git merge origin/master is equivalent to $ git pull if you're in master branch. We'll discuss more of this later.
  188. 188. Now that we understand the basic idea behind remote repos, let's proceed to Github... Sign-up at https://github.com
  189. 189. Create a repo in Github for devcon-git101 https://github.com/new
  190. 190. Pushing devcon-git101 Go back to devcon-git101 and push it to your new repo (this uses SSH url, you can use HTTPS) $ git remote add origin git@github.com:user/devcon-git101.git $ git push -u origin master Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts. Enter passphrase for key '/c/Users/user/.ssh/id_rsa': Counting objects: 40, done. Delta compression using up to 4 threads. Compressing objects: 100% (24/24), done. Writing objects: 100% (40/40), 3.41 KiB, done. Total 40 (delta 3), reused 0 (delta 0) To git@github.com:user/devcon-git101.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
  191. 191. git push Note that "git push" only pushes a single branch. If you want to push all branches you can use: $ git push REMOTE --all If you want to push all tags you can use: $ git push REMOTE --tags
  192. 192. Clone into another folder $ cd .. $ git clone git@github.com:user/devcon-git101.git github-git101 Cloning into 'github-git101'... remote: Counting objects: 40, done. remote: Compressing objects: 100% (21/21), done. remote: Total 40 (delta 3), reused 40 (delta 3) Receiving objects: 100% (40/40), done. Resolving deltas: 100% (3/3), done. done.
  193. 193. play time With the cloned folder, you can now play around with certain collaboration scenarios. For example: 1. Make some changes in /devcon-git101 2. Commit it via "git commit" 3. Push it to the repo at Github via "git push" 4. Go to /github-git101 and pull the changes via "git pull"
  194. 194. Want to let someone else push to your repo? Go to the repo Settings → Collaborators and add your friends.
  195. 195. Push/Pull You'll quickly notice that git push only allows fast-forward changes to be pushed. In simpler terms, you cannot push until you pull the latest changes from the remote.
  196. 196. pull + rebase Want to get rid of the distracting merge commits whenever you pull when you have pending commits? Tell git to rebase instead of merging: $ git pull --rebase origin master
  197. 197. Github for Windows automatically uses git pull --rebase + git push when syncing
  198. 198. merge vs rebase The problem with rebase is that you can overwrite commits already on the remote repository and this can affect your team. That said, people generally use rebase when working together on a single branch (e.g. git pull --rebase origin master) and merge when merging branches.
  199. 199. Tired of entering your password? There are multiple ways to authenticate users in Git. Let's discuss the more secure alternative: via SSH keys.
  200. 200. Cryptography pasakalye...
  201. 201. Authentication via Password Client Server
  202. 202. Authentication via Password Client sends credentials Server verifies username + password password DB
  203. 203. Authentication via Password Client sends credentials Server verifies username + password password DB can be intercepted e.g. FTP
  204. 204. Authentication via SSH Key (oversimplified) Client has 2 "keys" Server private key public key
  205. 205. Authentication via SSH Key (oversimplified) Client has 2 "keys" Server has a list of authorized keys private key public key
  206. 206. Authentication via SSH Key (oversimplified) Client has 2 "keys" Server has a list of authorized keys private key public key First, the public key must be sent to the server securely beforehand.
  207. 207. Authentication via SSH Key (oversimplified) Client Server has a list of authorized keys private key public key Using the private key, client can create a package that can only be unlocked by the corresponding public key and only that public key.
  208. 208. Authentication via SSH Key (oversimplified) Client Server has a list of authorized keys private key public key Using the private key, client can create a package that can only be unlocked by the corresponding public key and only that public key.
  209. 209. Authentication via SSH Key (oversimplified) Client Server has a list of authorized keys private key public key Using the private key, client can create a package that can only be unlocked by the corresponding public key and only that public key.
  210. 210. Authentication via SSH Key (oversimplified) Client Server has a list of authorized keys private key public key Using the private key, client can create a package that can only be unlocked by the corresponding public key and only that public key. doesn't matter if intercepted since they still need to crack it.
  211. 211. How to generate your SSH Keys: https://help.github.com/articles/generating-ssh-keys Also, follow the steps to add your public key to Github.
  212. 212. If you want to be secure, use a passphrase. However, if you're using Windows, you will need to enter it every time you run a git command that connects to Github.
  213. 213. Fortunately, there is a workaround that will only require you to enter it once per session. http://stackoverflow.com/a/9011152
  214. 214. Integrating Git with your Projects
  215. 215. Hosting Solutions
  216. 216. Free Private Repos Since Github is only free for public repos, you might want to look into the following to keep your source code private: Assembla (https://www.assembla.com) - 1 private repo, limited to 3 collaborators BitBucket (https://bitbucket.org/) - Unlimited private repos, limited to 5 collaborators Github Educational Accounts (https://github.com/edu) - free accounts for students and teachers
  217. 217. Self-Hosted Repos If you've got spare time and spare servers, you can also host your own git repositories: GitLab (https://www.gitlab.com/) - GitHub clone written in Ruby GitBucket (https://github.com/takezoe/gitbucket) - GitHub clone written in Scala You can also read the docs for other self-hosting options http://git-scm.com/book/en/Git-on-the-Server
  218. 218. Project Workflow
  219. 219. Project Workflow Here's the most basic workflow for working with with others through version control systems: 1. Double check if the project work e.g. compiles, pages load, etc. 2. Stage and commit your changes. 3. Before pushing, pull changes from the remote project repo. If there are no changes, skip to step 5. 4. Resolve conflicts, if any, then go back to step 1. 5. Push your changes.
  220. 220. Branches + Workflow There are two main camps in using branches for projects using Git: 1. Mainline e.g. git-flow (http://nvie.com/posts/a-successful-git-branching-model/) 2. Trunk-based e.g. how Github does it (https://gist.github.com/17twenty/6733076)
  221. 221. Branches + Workflow Client - Server VCS vs Distributed VCS
  222. 222. Branches + Workflow Push-based vs Pull-based
  223. 223. Communicate! Version control systems aims to improve communication between team members, not replace it. Always consult with the other person whenever you encounter a merge conflict.
  224. 224. Broken Builds To repeat step 1: 1. Double check if the project work e.g. compiles, pages load, etc. Broken builds are what we call published commits that have obvious critical, show-stopping bugs. That said, it's better to delay pushing your commits to spend more time making sure that the project works rather than waste everyone's time.
  225. 225. Continuous Integration You can use CI servers to automatically inform you of broken builds. Most CI servers nowadays support Git and its post-commit hooks to automatically test on a push. Some examples: ● Jenkins Git Plugin https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin ● TeamCity Git docs http://confluence.jetbrains.com/display/TCD8/Git+(JetBrains)
  226. 226. Work in Progress Avoid publishing half-done work as it can lead to broken builds. If you need to push those changes (e.g. you want a backup), put them in a branch then push that branch. Or consider using git stash.
  227. 227. Backups Don't rely on Git as a backup scheme; always have an external backup
  228. 228. Deployment
  229. 229. Deployment Automation e.g. Capistrano http://capistranorb.com/
  230. 230. Push-based Platform-as-a-Service e.g. Pagoda Box https://pagodabox.com/, Heroku https://www.heroku.com/
  231. 231. Github stuff
  232. 232. Github Webhooks https://developer.github.com/webhooks/
  233. 233. GitHub as a Project Management Tool
  234. 234. GitHub as a Project Management Tool for Open Source Projects
  235. 235. GitHub as a Portfolio
  236. 236. Summary of Commands git branch - list, create, or delete branches git checkout - checkout a branch or a path git merge - merge two or more branches git rebase - move commits to the end of a branch git clone - make a local copy of a repository git remote - list, create, or delete remote repos git fetch - retrieve objects/changes from a repository git pull - fetch + merge or rebase git push - publish local commits to a remote
  237. 237. Summary of Command Variations git checkout -b - create and checkout branch git remote add - add a new remote repository to track git remote rm - remove remote repository git pull --rebase - rebase instead of merge when pulling
  238. 238. Commands that I use everyday git pull --rebase git add git commit -am "blah blah" gitk / git log / git status / git diff git push
  239. 239. Commands that I use less often git clone git remote add git remote -v git checkout <branch> git rebase <branch> / git merge <branch> git checkout -- <path> git reset --hard <hash> git revert
  240. 240. Thank You For Listening!

×