Version Control System
Ynon Perek
Tuesday, March 6, 2012
Slide 2
Slide 2 text
Working On A Project
!"" include
# !"" entry.h
# $"" user.h
!"" lib
!"" resources
$"" src
!"" entry.c
!"" main.c
$"" user.c
!"" include
# !"" entry.h
# !"" photo.h
# $"" user.h
!"" lib
!"" resources
# $"" default.png
$"" src
!"" entry.c
!"" main.c
!"" photo.c
$"" user.c
Tuesday, March 6, 2012
Slide 3
Slide 3 text
Working On A Project
• In a managed project, we
have virtual “save points”
of the entire file system
• Think old “quest” games
Tuesday, March 6, 2012
Slide 4
Slide 4 text
Reasons For VCS
• Go back to a better previous state
• Backup everything
• Change is your friend
• Collaboration
Tuesday, March 6, 2012
Slide 5
Slide 5 text
Collaboration
• Everyone knows
what others are
working on
• Easily integrate
other developer’s
work
Tuesday, March 6, 2012
Slide 6
Slide 6 text
Same File Collaboration
Tuesday, March 6, 2012
Slide 7
Slide 7 text
Assume The Following
int add(int x, int y) {
return x + y;
}
int deleteFile(char *filename) {
// delete a file
}
int add(int x, int y) {
// adds x and y
return x + y;
}
int deleteFile(char *filename) {
}
Bob Alice
Tuesday, March 6, 2012
Slide 8
Slide 8 text
Merged Work
int add(int x, int y) {
// adds x and y
return x + y;
}
int deleteFile(char *filename) {
// delete a file
}
• Both
developers’
work is in the
file
Tuesday, March 6, 2012
Slide 9
Slide 9 text
Centralized / Distributed
All data is stored on the server All data is stored locally
A client keeps a ‘workspace’, only
a single state
Each client keeps entire project
history
Server is required for every
operation
Server is only required for
collaboration
Tuesday, March 6, 2012
Slide 10
Slide 10 text
Available VCSs
• Subversion, cvs, perforce, ClearCase,
SourceSafe
• Git, Mercurial, Bazaar
• Let’s talk about git
Tuesday, March 6, 2012
Slide 11
Slide 11 text
“Cool Kids” Version control system
git
Tuesday, March 6, 2012
Slide 12
Slide 12 text
git
“Git
-noun
British Slang. an Unpleasant or
contemptible person”
-Oxford English Dictionary
Tuesday, March 6, 2012
Slide 13
Slide 13 text
I’m an egotistical
bastard, and I name
all my projects after
myself.
First Linux, now git
Tuesday, March 6, 2012
Slide 14
Slide 14 text
Working With Git
• Set up a repository
• Make changes and commit
• Access and diff with past versions
• Time Wraps (branching)
• Collaboration
Tuesday, March 6, 2012
Slide 15
Slide 15 text
Setting up the Repo
• Create a new
repository
• All repository
data is stored
in .git folder
cd myproject
git init
Tuesday, March 6, 2012
Slide 16
Slide 16 text
Project Changelist
• A change list represents changes in a
project from state A to state B
• Changes include:
• New files
• Deleted (removed) files
• Modified files
Tuesday, March 6, 2012
Slide 17
Slide 17 text
Editing Changelist
# Add main.c to the changelist
git add main.c
# Add all files in current directory to the changelist
git add .
# Add all .cpp files to the change list
git add *.cpp
# Insert a Remove File command to the change list
git rm mistake.cpp
# Insert a Rename command to the change list
git mv persons.cpp people.cpp
Tuesday, March 6, 2012
Slide 18
Slide 18 text
Editing Changelist
# Add all .c files to the change list
git add *.c
# Remove main.c from the change list
git reset main.c
Tuesday, March 6, 2012
Slide 19
Slide 19 text
Committing A Changelist
• Use git commit to save a changelist
to the local repository
• Use git reset --hard to restore
last saved version
Tuesday, March 6, 2012
Getting Status
> git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
#! renamed: include/entry.h -> include/contact.h
#! renamed: src/entry.c -> src/contact.c
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
#! include/photo.h
#! resources/
#! src/photo.c
> git commit
Tuesday, March 6, 2012
Slide 22
Slide 22 text
Getting Status
> git log
commit 6a0cf56dfbbd8334c28a8ee5b2f88ced42cb8fa2
Author: ynonp
Date: Thu Feb 16 07:46:18 2012 +0200
New Feature: User photos
commit d70f574d2f6a3a67de69ee58a2e27daff4c6b10e
Author: ynonp
Date: Thu Feb 16 07:40:56 2012 +0200
First commit
Tuesday, March 6, 2012
Slide 23
Slide 23 text
Getting Status
• Use git status to get current
directory status
• Use git log to list history
• In the log, each entry has a commit
number. Use
git checkout d70f
To go back a given commit number
Tuesday, March 6, 2012
Slide 24
Slide 24 text
Checking Out
• git checkout takes a commit number
and temporarily restore state to that commit
• Making changes to a past state will open a
new branch
• Use git checkout master to go back
to head
• Always commit or reset changes before
checking out
Tuesday, March 6, 2012
Slide 25
Slide 25 text
Past: In Depth
• Use git diff d70f to diff with
previous version (in this case d707)
• Use git show d70f:main.c to
show file main.c from commit d707
Tuesday, March 6, 2012
Slide 26
Slide 26 text
Q & A
• Set up a repository
• Make changes and commit
• Access and diff with past versions
• Time Wraps (branching)
• Collaboration
Tuesday, March 6, 2012
Slide 27
Slide 27 text
Branching
• Context switching is inevitable
• A friend needs your help for a version
she’s working on
• Working on a new feature, a new bug
was found in a released version
Tuesday, March 6, 2012
Slide 28
Slide 28 text
Branching
Work (A)
Work (B)
Work (C)
Work (B)
Work (B’)
Work (B’’)
Work (D)
Tuesday, March 6, 2012
Slide 29
Slide 29 text
Branching Commands
• Use git branch feature to create
a new branch named feature
• Use git checkout feature to
switch to the new branch
• Use git checkout -b d70f to start
a new branch from old commit
• Use git branch for info
Tuesday, March 6, 2012
Slide 30
Slide 30 text
Branching Flow
# Working on the project, when suddenly a client calls.
# She found a bug in release d70f
# Commit all active work
git commit -a
# Create a new branch called fixes starting from commit d70f
git checkout -b fixes d70f
# Fix bug ...
# Commit your newly fixed code
git commit -a
# Get back to work
git checkout master
Tuesday, March 6, 2012
Slide 31
Slide 31 text
Merging
• Use git merge fixes to merge the
branch named fixes into current branch
• Use git branch -d fixes to delete
the branch named fixes after you’re
done with it
Tuesday, March 6, 2012
Slide 32
Slide 32 text
Q & A Branches
Tuesday, March 6, 2012
Slide 33
Slide 33 text
Gitting Together
• A central repository is used to
synchronize multiple developers
• Each developer sees the repository as a
remote branch
• Commits can be pushed to the remote
Tuesday, March 6, 2012
Slide 34
Slide 34 text
Gitting Together
• Create an empty central repository
• Fill it with some initial data
• Have other devs clone the repository
Tuesday, March 6, 2012