Incorporating Version Control into Programming Courses

9e4d3e53f8525fdff00691a8b843e66b?s=47 Tommy MacWilliam
March 07, 2013
99

Incorporating Version Control into Programming Courses

9e4d3e53f8525fdff00691a8b843e66b?s=128

Tommy MacWilliam

March 07, 2013
Tweet

Transcript

  1. Incorporating Version Control into Programming Courses Tommy MacWilliam tmacwilliam@cs.harvard.edu

  2. None
  3. Source Control Management

  4. What? • track revisions of files • review past versions

    of projects • collaborate with peers and staff
  5. What? • CVS • SVN • Git • Mercurial •

    Bazaar • ...
  6. Students: Why? • “I need to work on a project

    with a partner”
  7. Students: Why? • “I need to work on a project

    with a partner” • “It was working a few hours ago, but I broke it”
  8. Students: Why? • “I need to work on a project

    with a partner” • “It was working a few hours ago, but I broke it” • “I want to back up my code so I can work on another machine”
  9. Staff: Why? • “I want to collect homework submissions on

    my server”
  10. Staff: Why? • “I want to collect homework submissions on

    my server” • “I want to allow students to review each others’ code”
  11. Staff: Why? • “I want to collect homework submissions on

    my server” • “I want to allow students to review each others’ code” • “I want to distribute updates to distribution code”
  12. Vocabulary • repository: project containing source code files • commit:

    snapshot of a project in time • log: history of commits for a project • branch: independent set of changes • remote: server hosting code • tag: human-readable name for a commit
  13. SCM Tools • centralized • all commits go to one

    repository • decentralized • commits go to developers’ individual repositories
  14. SVN

  15. SVN with Google Code

  16. None
  17. svn checkout https://project.googlecode.com/svn/trunk/ project --username you@gmail.com

  18. Generated Password

  19. https://code.google.com/hosting/settings

  20. Repository Conventions • trunk: source code • branches: independent sets

    of changes • tags: named commits
  21. svn add pset1.c

  22. svn rm pset1.c

  23. svn status

  24. svn commit -m “done!”

  25. svn update

  26. svn log

  27. Workflow • student creates and checks out repository • student

    adds, commits files to repository • staff updates from repository to view files
  28. Basic SVN Workflow • create repository • svn checkout https://project.googlecode.com/svn/

    • edit pset1.c • svn add pset1.c • svn commit -m “done!”
  29. Practice! • create a new SVN repository called yourname-sigcse •

    checkout the repository to ~/svn/student1 • create a file called pset1.c • add and commit pset1.c to the pset1 repo • add and commit test.c to the pset1 repo • view the history of commits
  30. svn diff

  31. svn diff -r 1:2

  32. Practice! • make changes to pset1.c without committing, then view

    the changes • commit the changes and compare to previous revision • compare the last revision to the first revision • search all commit logs for the word “done”
  33. svn checkout -r 123

  34. svn update -r 123

  35. Practice! • roll back to the first revision • check

    out a new copy of pset1, starting at the second revision
  36. SVN Collaboration • check out pset1 (again) to ~/svn/student2 •

    make a change and commit as student2 • update the repository as student1
  37. Alice svnadmin create pset1 Bob

  38. Alice svn co https://host/pset1 Bob

  39. Alice svn co https://host/pset1 Bob

  40. Alice svn co https://host/pset1 Bob

  41. Alice svn co https://host/pset1 Bob

  42. Alice svn commit -m “done” Bob

  43. Alice svn commit -m “done” Bob

  44. Alice svn up Bob

  45. Alice svn up Bob

  46. Merge Conflicts • students commit incompatible changes • cannot commit

    until conflicts are resolved
  47. Creating a Merge Conflict • student1 edits pset1.c to say

    “here comes a conflict” • student1 commits to pset1 repo • student2 edits pset1.c to say “I don’t like conflict” • student2 tries to commit
  48. Conflict discovered in '~/svn/student2/pset1/file.c'. Select: (p) postpone, (df) diff-full, (e)

    edit, (mc) mine-conflict, (tc) theirs-conflict, (s) show all options: d
  49. Resolving a Merge Conflict • don’t panic! • mc: keep

    my changes to the file • tc: keep their changes to the file • e: manually edit conflicted file • p: view files that caused conflict separately
  50. <<<<<<< this is student1 ======= this is student2 >>>>>>> Single

    File Resolution
  51. jharvard@appliance (~/svn/student2/pset1): ls file.c file.c.mine file.c.r3 Multiple File Resolution

  52. Resolving a Merge Conflict • don’t panic!

  53. Practice! • edit student1/pset1.c to say “here comes a conflict”

    • commit from student1/pset1 • edit student2/pset1.c to say “I don’t like conflict” • commit from student2/pset1 • press “e” to edit as a single file • repeat, but press “p” to resolve conflict!
  54. Tags • human-readable aliases for commits • software releases, betas,

    submissions
  55. svn cp https://project.googlecode.com/svn/trunk/ https://project.googlecode.com/svn/tags/1.0

  56. Branches

  57. Branches • one partner wants to try something out

  58. Branches • one partner wants to try something out •

    develop a feature independently of project
  59. Branches • one partner wants to try something out •

    develop a feature independently of project • staff make changes during feedback process
  60. svn cp https://project.googlecode.com/svn/trunk/ https://project.googlecode.com/svn/branches/test

  61. Branches • independent sets of changes • changes on one

    branch don’t affect other branches • switching between branches changes working copy
  62. svn switch https://project.googlecode.com/svn/branches/test

  63. svn rm https://project.googlecode.com/svn/branches/test

  64. Practice! • create a branch called “test” • switch to

    the branch • make changes and commit • switch back to trunk • delete branch “test”
  65. SVN on GitHub

  66. SVN on GitHub • svn co --depth empty https://github.com/you/project •

    svn up trunk • edit files, svn add, svn commit
  67. Git

  68. git config --global user.name “Tommy MacWilliam”

  69. git config --global user.email tmacwilliam@cs.harvard.edu

  70. git init

  71. git add pset1.c

  72. git add --all

  73. git status

  74. git commit -m “done!”

  75. git log

  76. 5aeebab117b892fa42002146e4c62be676bc4621 b43b0ad1e8108e7ab870d7a54feac93ae8b8600e 461476587780aa9fa5611ea6dc3912c146a91760 Commit ID

  77. 5aeebab117b892fa42002146e4c62be676bc4621 b43b0ad1e8108e7ab870d7a54feac93ae8b8600e 461476587780aa9fa5611ea6dc3912c146a91760 Commit ID HEAD

  78. Practice! • create a new repository in ~/git/pset1 • create

    a file called pset1.c • add and commit pset1.c • make more changes to pset1.c • add and commit pset1.c again
  79. Git on GitHub

  80. SSH Keys

  81. ssh-keygen

  82. None
  83. git clone git@github.com:you/project

  84. git remote

  85. git remote add origin url

  86. git push origin master

  87. Basic Git Workflow • staff (or students) create remote repositories

    • students • clone repository • add and commit changes • push changes to remote repository
  88. Practice! • create a GitHub repository • clone the repository

    • create pset1.c, then add and commit it • push pset1.c to the remote repository
  89. git show

  90. git show b43b0

  91. git diff HEAD

  92. git diff b43b0 5aeeb

  93. Practice! • make changes to pset1.c without committing, then view

    the changes • commit the changes and compare to previous revision • compare the last revision to the first revision • search all commit logs for the word “done”
  94. git checkout b43b0

  95. git checkout b43b0 pset1.c

  96. git checkout master

  97. git reset --hard

  98. Practice! • roll back to the first revision • roll

    only one file back to the second revision • fast-forward to the current state again
  99. git revert b43b0

  100. 5aeeb b43b0 46147

  101. 5aeeb b43b0 46147 a45bc git revert 46147

  102. Practice! • undo the changes in your last commit •

    undo the commit that undoes that commit
  103. git tag done

  104. git push --tags

  105. git tag

  106. git tag -l “1.*”

  107. Practice! • create a tag called “1.0” • commit new

    changes • create a tag called “1.1” • view tags
  108. git branch

  109. git branch test

  110. git checkout test

  111. 5aeeb b43b0 46147 master

  112. 5aeeb b43b0 46147 master f862f 36223 test git branch test

  113. git checkout master

  114. git merge

  115. 5aeeb b43b0 46147 f862f 36223 git branch test a34bc git

    merge test
  116. git branch -D test

  117. Practice! • create a branch called “test” • switch to

    the test branch • make and commit changes • switch to the master branch • merge changes from the test branch • delete the test branch
  118. 5aeeb b43b0 46147 f862f 36223 master test git branch test

  119. 5aeeb b43b0 46147 f862f 36223 master git rebase

  120. Practice! • create a branch called “test2” • switch to

    the test2 branch • make and commit changes • switch to the master branch • rebase changes from the test2 branch • delete the test2 branch
  121. Collaborating with Git • students have their own local repositories

    • students commit, branch, etc. on local repositories • students push to and pull from a shared repository
  122. git pull origin master

  123. git pull --rebase

  124. Alice Bob git init git add --all git commit

  125. Alice git remote add origin url git push origin master

    Bob
  126. Alice git remote add origin url git push origin master

    Bob
  127. Alice git clone url Bob

  128. Alice git clone url Bob

  129. Alice git add --all git commit Bob

  130. Alice git push origin master Bob

  131. Alice Bob git push origin master

  132. Alice git pull origin master Bob

  133. Alice Bob git pull origin master

  134. Practice! • create a pset2 repository on GitHub • clone

    the repository in ~/git/student1/pset2 • add, commit, and push a change • clone the repository in ~/git/student2/pset2 • add, commit, and push a change • pull the change from ~/git/student1/pset2
  135. Merge Conflicts • don’t panic! • git status shows conflicted

    files • git add files to resolve conflicts
  136. Resolving Conflicts <<<<<<< this is from test ======= this is

    from master >>>>>>>
  137. Creating a Merge Conflict • student1 edits pset2.c to say

    “here comes a conflict” • student1 commits and pushes to pset2 repo • student2 edits pset2.c to say “I don’t like conflict” • student2 commits, tries to push
  138. Resolving using Merge • fix conflicts • git add --all

    • git commit -m “merge”
  139. Resolving using Rebase • fix conflicts • git add --all

    • git rebase --continue
  140. Git on BitBucket

  141. Using SCM in Programming Courses

  142. Using Google Code • each student creates repository for each

    project • students add staff as collaborators
  143. None
  144. Using GitHub • https://github.com/edu • each student creates repository for

    each project • students add staff as collaborators
  145. None
  146. Using BitBucket • unlimited free private repositories! • same process

    as GitHub
  147. Hosting SVN • svnadmin create --fs-type fsfs pset1 • repo/conf/svnserve.conf

    • svnserve
  148. Hosting Git • https://github.com/sitaramc/gitolite • http://scie.nti.st/2007/11/14/hosting-git- repositories-the-easy-and-secure-way/

  149. Repository-Based • each project is a separate repository • students

    collaborate on same hosted repository • if projects are totally separate, makes sense • if projects build on each other, makes less sense
  150. Branch-Based • each student creates one repository for the course

    • each project is a different branch • if projects build on each other, makes sense • if projects are totally separate, makes less sense
  151. Distributing Code • each student: • create repository on GitHub

    • git clone git://github.com/course/pset1.git • git remote rm origin • git remote add origin git@github.com/student/ pset1.git • git remote add distro git://github.com/course/ pset1.git
  152. Submitting Code • students • git tag submission • git

    push --tags • staff • git checkout submission
  153. When Disaster Strikes • svn revert --recursive . • git

    reset --hard
  154. When Disaster Really Strikes • rm -rf .git • find

    . -type d -name .svn -exec rm -rf {} \;
  155. Potential Pitfalls • merge conflicts prevent pushes! • don’t panic

    • one minute before the deadline, still don’t panic • distinguish between submission and version control? • have a backup submission plan!
  156. Potential Pitfalls • commit timestamps are set by the client!

    • tags can be deleted and moved around • if new to SCM, submission process is very complicated • if using SCM, then teach SCM
  157. Hooks • small scripts that are triggered by SCM events

    • post-receive: whenever a push is received
  158. Automating Processes • http://developer.github.com/v3/ • https://confluence.atlassian.com/display/ BITBUCKET/Using+the+Bitbucket+REST +APIs

  159. version50

  160. gem install version50

  161. v50 create

  162. v50 status

  163. v50 save

  164. v50 history

  165. v50 warp

  166. v50 recover

  167. v50 download

  168. speakerdeck.com/tmacwill

  169. Incorporating Version Control into Programming Courses Tommy MacWilliam tmacwilliam@cs.harvard.edu