8 Version Control
8 Version Control
1
Outline
1. Review
2. Version Control
3. Git
2
Outline
1. Review
2. Version Control
3. Git
3
Computer Networks
4
Computer Networks
4
Files
5
Files
5
Files
5
Files
5
Outline
1. Review
2. Version Control
2.1 Version Control Systems
2.2 Comparison of VCSs
3. Git
6
Outline
1. Review
2. Version Control
2.1 Version Control Systems
2.2 Comparison of VCSs
3. Git
7
Version Control Systems
8
Version Control Systems
8
Version Control Systems
8
Version Control Systems
9
Version Control Systems
10
Version Control Systems
10
Version Control Systems
10
Version Control Systems
10
Version Control Systems
10
Version Control Systems
1. Review
2. Version Control
2.1 Version Control Systems
2.2 Comparison of VCSs
3. Git
11
Google Docs
You can make a bunch of copies of files or folders with cp as a simple form of
version control. You can compare versions with diff .
Pros:
• Works on either rich or plain text (or anything else)
• It's simple and makes it easy to move data between versions
Cons:
• It's messy and a lot of manual work
• It's hard to tell what the relationship between different versions is
• It takes a lot of hard drive space
13
Zip Files
Instead of just cp ing folders, we could bundle them up into a Zip file (a single file
which can be “unzipped” into a folder).
Pros:
• Tracks versions for an entire folder at once
• Easy to share a version with someone else (email)
Cons:
• It's still a lot of manual work
• It's hard to tell what the relationship between different versions is
• It's hard to extract a single file from an old version
14
Zip Files++
• What if we had a tool which did all this zip file stuff automatically?
15
Zip Files++
• What if we had a tool which did all this zip file stuff automatically?
• We could tell it to take a “snapshot” of a directory, and it would save all the
changes in it.
15
Zip Files++
• What if we had a tool which did all this zip file stuff automatically?
• We could tell it to take a “snapshot” of a directory, and it would save all the
changes in it.
• We could ask it to recover an old version of a specific file, or to reset everything
to an old version to “undo” our work.
15
Zip Files++
• What if we had a tool which did all this zip file stuff automatically?
• We could tell it to take a “snapshot” of a directory, and it would save all the
changes in it.
• We could ask it to recover an old version of a specific file, or to reset everything
to an old version to “undo” our work.
• The tool could track the relationships between different versions, so we can
have multiple “current” versions at the same time.
15
Zip Files++
• What if we had a tool which did all this zip file stuff automatically?
• We could tell it to take a “snapshot” of a directory, and it would save all the
changes in it.
• We could ask it to recover an old version of a specific file, or to reset everything
to an old version to “undo” our work.
• The tool could track the relationships between different versions, so we can
have multiple “current” versions at the same time.
• If we want to combine different versions, the tool can automatically do it for us
(instead of us copying and pasting the parts together).
15
Git
16
Git
16
Git
16
Git
16
Git
16
Git
1. Review
2. Version Control
3. Git
3.1 Linear History
3.2 Branching Workflow
3.3 Combining Branches
17
Outline
1. Review
2. Version Control
3. Git
3.1 Linear History
3.2 Branching Workflow
3.3 Combining Branches
18
Basic Workflow
The simplest way to use git is the “linear” workflow, which is the same way you'd use
Google Docs:
19
Basic Workflow
The simplest way to use git is the “linear” workflow, which is the same way you'd use
Google Docs:
1. git init to enable Git in a certain directory
19
Basic Workflow
The simplest way to use git is the “linear” workflow, which is the same way you'd use
Google Docs:
1. git init to enable Git in a certain directory
2. git add any files you want Git to “track”
19
Basic Workflow
The simplest way to use git is the “linear” workflow, which is the same way you'd use
Google Docs:
1. git init to enable Git in a certain directory
2. git add any files you want Git to “track”
3. git commit the currently “staged” changes to save a snapshot
19
Basic Workflow
The simplest way to use git is the “linear” workflow, which is the same way you'd use
Google Docs:
1. git init to enable Git in a certain directory
2. git add any files you want Git to “track”
3. git commit the currently “staged” changes to save a snapshot
4. make changes to your files
19
Basic Workflow
The simplest way to use git is the “linear” workflow, which is the same way you'd use
Google Docs:
1. git init to enable Git in a certain directory
2. git add any files you want Git to “track”
3. git commit the currently “staged” changes to save a snapshot
4. make changes to your files
5. git add the changed files to “stage” them again
19
Basic Workflow
The simplest way to use git is the “linear” workflow, which is the same way you'd use
Google Docs:
1. git init to enable Git in a certain directory
2. git add any files you want Git to “track”
3. git commit the currently “staged” changes to save a snapshot
4. make changes to your files
5. git add the changed files to “stage” them again
6. Repeat from 3
You can use git log to see your commit history, and use git status to see the
current state of staged/unstaged/untracked changes. 19
Basic Workflow
Demo
Let's practice how to:
• Create a new Git repository
• Commit a new file
• Commit changes to files
• Revert commits
• Look at an old version of a file
• Compare two versions of files
• See your commit history
20
Outline
1. Review
2. Version Control
3. Git
3.1 Linear History
3.2 Branching Workflow
3.3 Combining Branches
21
Branching Workflow
We can also split our “repo” into multiple BRANCHES, which are like alternate
versions of a folder. This means different people can work on different things
without interfering with one another.
22
Branching Workflow
We can also split our “repo” into multiple BRANCHES, which are like alternate
versions of a folder. This means different people can work on different things
without interfering with one another.
1. Make sure your repository is “clean” (i.e., you have no uncommitted changes).
22
Branching Workflow
We can also split our “repo” into multiple BRANCHES, which are like alternate
versions of a folder. This means different people can work on different things
without interfering with one another.
1. Make sure your repository is “clean” (i.e., you have no uncommitted changes).
2. git checkout -b <branch> to create a new branch and move to it; at this
point, the new branch will be identical to the old one.
3. Make changes, git add , git commit as usual
4. git checkout to switch between branches
22
Outline
1. Review
2. Version Control
3. Git
3.1 Linear History
3.2 Branching Workflow
3.3 Combining Branches
23
Branching Workflow
Combining Branches
Now that we have multiple branches, we probably want to join them back together
at some point.
There are several ways to do this:
• git merge two branches into one
• git merge --fast-forward a long branch onto a shorter version of itself
• git rebase one branch onto another branch
• git cherry-pick a specific commit from one branch to another
24
Branching Workflow
Fast Forwarding
A B C
Branching Workflow
Fast Forwarding
A B C
X Y
Branching Workflow
Fast Forwarding
A B C X Y
Branching Workflow
Fast Forwarding
A B C X Y F
25
Branching Workflow
Merging
MERGING (in general) creates a MERGE COMMIT to join the two branches.
A B C
Branching Workflow
Merging
MERGING (in general) creates a MERGE COMMIT to join the two branches.
A B C
X Y
Branching Workflow
Merging
MERGING (in general) creates a MERGE COMMIT to join the two branches.
A B C D E
X Y
Branching Workflow
Merging
MERGING (in general) creates a MERGE COMMIT to join the two branches.
A B C D E M
X Y
Branching Workflow
Merging
MERGING (in general) creates a MERGE COMMIT to join the two branches.
A B C D E M F
X Y
26
Branching Workflow
Rebasing
A B C
Branching Workflow
Rebasing
A B C
X Y
Branching Workflow
Rebasing
A B C D E
X Y
Branching Workflow
Rebasing
A B C D E
X' Y'
Branching Workflow
Rebasing
A B C D E X' Y'
Branching Workflow
Rebasing
A B C D E X' Y' F
Branching Workflow
Rebasing
A B C D E X' Y' F
27
Branching Workflow
Cherry-Picking
A B C
Branching Workflow
Cherry-Picking
A B C
X Y Z
Branching Workflow
Cherry-Picking
A B C D E
X Y Z
Branching Workflow
Cherry-Picking
A B C D E Y'
X Y Z
Branching Workflow
Cherry-Picking
A B C D E Y' F
X Y Z
Branching Workflow
Cherry-Picking
A B C D E Y' F
X' Z'
Branching Workflow
Cherry-Picking
A B C D E Y' F
X' Z'
28
Branching Workflow
When to merge/rebase/cherry-pick?
• fast-forward when possible ( git merge --ff-only ).
• rebase and then fast-forward if possible, i.e., if you're the only one working on
the branch; never rebase a branch other people are using ( git rebase and
git merge --ff-only ).
• merge if neither of the above are possible ( git merge ).
• cherry-pick if you want to copy a specific commit to another branch
( git cherry-pick )1 .
1
This is pretty rare, I’ve only used it a handful of times.
29
Branching Workflow
Branching Demo
30
To Be Continued…
We'll pick back up with merge conflict resolution and collaboration in Lecture 9.
Some commands which came up during class:
git checkout: essentially means “move to a different commit”; doesn't change
your git history
git reset: “resets” the entire repository to the way it was in an old commit (and
changes git history to match)
git revert: “undoes” a specific old commit by creating a new commit that does
the opposite
Note that, even though Git commits are technically versions, Git's commands often
operate on the changes between versions.
31