Students: Why?
• “I need to work on a project with a
partner”
Slide 7
Slide 7 text
Students: Why?
• “I need to work on a project with a
partner”
• “It was working a few hours ago, but I
broke it”
Slide 8
Slide 8 text
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”
Slide 9
Slide 9 text
Staff: Why?
• “I want to collect homework submissions
on my server”
Slide 10
Slide 10 text
Staff: Why?
• “I want to collect homework submissions
on my server”
• “I want to allow students to review each
others’ code”
Slide 11
Slide 11 text
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”
Slide 12
Slide 12 text
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
Slide 13
Slide 13 text
SCM Tools
• centralized
• all commits go to one repository
• decentralized
• commits go to developers’ individual
repositories
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
Slide 30
Slide 30 text
svn diff
Slide 31
Slide 31 text
svn diff -r 1:2
Slide 32
Slide 32 text
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”
Slide 33
Slide 33 text
svn checkout -r 123
Slide 34
Slide 34 text
svn update -r 123
Slide 35
Slide 35 text
Practice!
• roll back to the first revision
• check out a new copy of pset1, starting at
the second revision
Slide 36
Slide 36 text
SVN Collaboration
• check out pset1 (again) to ~/svn/student2
• make a change and commit as student2
• update the repository as student1
Slide 37
Slide 37 text
Alice
svnadmin create pset1
Bob
Slide 38
Slide 38 text
Alice
svn co https://host/pset1
Bob
Slide 39
Slide 39 text
Alice
svn co https://host/pset1
Bob
Slide 40
Slide 40 text
Alice
svn co https://host/pset1
Bob
Slide 41
Slide 41 text
Alice
svn co https://host/pset1
Bob
Slide 42
Slide 42 text
Alice
svn commit -m “done”
Bob
Slide 43
Slide 43 text
Alice
svn commit -m “done”
Bob
Slide 44
Slide 44 text
Alice
svn up
Bob
Slide 45
Slide 45 text
Alice
svn up
Bob
Slide 46
Slide 46 text
Merge Conflicts
• students commit incompatible changes
• cannot commit until conflicts are resolved
Slide 47
Slide 47 text
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
Slide 48
Slide 48 text
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
Slide 49
Slide 49 text
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
Slide 50
Slide 50 text
<<<<<<<
this is student1
=======
this is student2
>>>>>>>
Single File Resolution
Slide 51
Slide 51 text
jharvard@appliance (~/svn/student2/pset1): ls
file.c file.c.mine file.c.r3
Multiple File Resolution
Slide 52
Slide 52 text
Resolving a Merge
Conflict
• don’t panic!
Slide 53
Slide 53 text
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!
5aeebab117b892fa42002146e4c62be676bc4621
b43b0ad1e8108e7ab870d7a54feac93ae8b8600e
461476587780aa9fa5611ea6dc3912c146a91760
Commit
ID
Slide 77
Slide 77 text
5aeebab117b892fa42002146e4c62be676bc4621
b43b0ad1e8108e7ab870d7a54feac93ae8b8600e
461476587780aa9fa5611ea6dc3912c146a91760
Commit
ID
HEAD
Slide 78
Slide 78 text
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
Basic Git Workflow
• staff (or students) create remote
repositories
• students
• clone repository
• add and commit changes
• push changes to remote repository
Slide 88
Slide 88 text
Practice!
• create a GitHub repository
• clone the repository
• create pset1.c, then add and commit it
• push pset1.c to the remote repository
Slide 89
Slide 89 text
git show
Slide 90
Slide 90 text
git show b43b0
Slide 91
Slide 91 text
git diff HEAD
Slide 92
Slide 92 text
git diff b43b0 5aeeb
Slide 93
Slide 93 text
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”
Slide 94
Slide 94 text
git checkout b43b0
Slide 95
Slide 95 text
git checkout b43b0
pset1.c
Slide 96
Slide 96 text
git checkout master
Slide 97
Slide 97 text
git reset --hard
Slide 98
Slide 98 text
Practice!
• roll back to the first revision
• roll only one file back to the second
revision
• fast-forward to the current state again
Slide 99
Slide 99 text
git revert b43b0
Slide 100
Slide 100 text
5aeeb
b43b0
46147
Slide 101
Slide 101 text
5aeeb
b43b0
46147
a45bc
git revert 46147
Slide 102
Slide 102 text
Practice!
• undo the changes in your last commit
• undo the commit that undoes that commit
Slide 103
Slide 103 text
git tag done
Slide 104
Slide 104 text
git push --tags
Slide 105
Slide 105 text
git tag
Slide 106
Slide 106 text
git tag -l “1.*”
Slide 107
Slide 107 text
Practice!
• create a tag called “1.0”
• commit new changes
• create a tag called “1.1”
• view tags
Slide 108
Slide 108 text
git branch
Slide 109
Slide 109 text
git branch test
Slide 110
Slide 110 text
git checkout test
Slide 111
Slide 111 text
5aeeb
b43b0
46147
master
Slide 112
Slide 112 text
5aeeb
b43b0
46147
master
f862f
36223
test
git branch test
Slide 113
Slide 113 text
git checkout master
Slide 114
Slide 114 text
git merge
Slide 115
Slide 115 text
5aeeb
b43b0
46147
f862f
36223
git branch test
a34bc
git merge test
Slide 116
Slide 116 text
git branch -D test
Slide 117
Slide 117 text
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
Slide 118
Slide 118 text
5aeeb
b43b0
46147
f862f
36223
master test
git branch test
Slide 119
Slide 119 text
5aeeb
b43b0
46147 f862f 36223
master
git rebase
Slide 120
Slide 120 text
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
Slide 121
Slide 121 text
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
Slide 122
Slide 122 text
git pull origin master
Slide 123
Slide 123 text
git pull --rebase
Slide 124
Slide 124 text
Alice Bob
git init
git add --all
git commit
Slide 125
Slide 125 text
Alice
git remote add origin url
git push origin master
Bob
Slide 126
Slide 126 text
Alice
git remote add origin url
git push origin master
Bob
Slide 127
Slide 127 text
Alice
git clone url
Bob
Slide 128
Slide 128 text
Alice
git clone url
Bob
Slide 129
Slide 129 text
Alice
git add --all
git commit
Bob
Slide 130
Slide 130 text
Alice
git push origin master
Bob
Slide 131
Slide 131 text
Alice Bob
git push origin master
Slide 132
Slide 132 text
Alice
git pull origin master
Bob
Slide 133
Slide 133 text
Alice Bob
git pull origin master
Slide 134
Slide 134 text
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
Slide 135
Slide 135 text
Merge Conflicts
• don’t panic!
• git status shows conflicted files
• git add files to resolve conflicts
Slide 136
Slide 136 text
Resolving Conflicts
<<<<<<<
this is from test
=======
this is from master
>>>>>>>
Slide 137
Slide 137 text
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
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
Slide 150
Slide 150 text
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
When Disaster Really
Strikes
• rm -rf .git
• find . -type d -name .svn -exec rm -rf {} \;
Slide 155
Slide 155 text
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!
Slide 156
Slide 156 text
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
Slide 157
Slide 157 text
Hooks
• small scripts that are triggered by SCM
events
• post-receive: whenever a push is received