Lesson 8: Version Control

Homepage Content Slides Video

Warning

This lesson is under construction. Learn from it at your own risk. If you have any feedback, please fill out our General Feedback Survey.

Overview

Version Control Systems

VCS is how one tracks changes, modifications, and updates to source files over time. Creating a history of changes for a project over time.

Used for:
  • Documentation
  • Code
  • Configuration
  • Collaboration
Other Names Include:
  • Source Control Management (SCM)
  • Version Control Software
  • Revision Control Software

What VCS Solves

PHD Comics 'Final Draft'.

What VCS Solves

Version control solves a lot of problems:
  • I have changes I want to integrate (merge) into the main project.
  • I want to track the state of this project over time.
  • I want to make some changes without possibly breaking what I have.
  • ... and much more.

Principles of VCS

Repository: A project.
Diff: Delta between two points in history.
Commit: A snapshot of project in time.
Branch: Modifications made in parallel with the main project.
Merge: Introducing changes from one branch into another.
Clone: Downloading a local copy of a project.
Fork: A modified version of an existing project.

Types of VCS

Centralized VCS

"I'm going to work at the designated work-bench. Nobody else can work on this until I'm done."

  • Subversion
  • CVS
Distributed VCS

"I'm going to work over here for a while and tell you about what I did later."

  • Git
  • Mercurial

Git

Git is a Free and Open Source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. ( https://git-scm.com )
Git logo

Setting up Git

$ git config --global user.name "My Name"
$ git config --global user.email "myself@gmail.com"
$ git config --global core.editor "nano"

TODO: Use Git Locally

Create a project with Git:

$ mkdir my-project
$ cd my-project    # Always run `git init` inside of a project folder!
$ git init         # Never inside of your home directory.

Add and commit a file to your project with Git:

$ touch newfile.txt
$ git add newfile.txt
$ git commit  # Edit message in Nano, save the file, exit to commit.

To see which files are staged, unstaged, or untracked:

$ git status

TODO: Use Git Locally

To look through your repository history:

$ git log

To create and checkout a branch:

#Note the `*` which indicates the current branch
$ git checkout -b "new-branch"
$ git branch
master
* new-branch

TODO: Working With a Git Repository

Checkout a new feature branch on your repository.

$ git checkout -b "add-awesome-feature"

Create/Edit files on the new branch.

$ echo "Some awesome text" > awesomefile.txt
$ git status
# On branch add-awesome-feature
# Untracked files:
...
#         awesomefile.txt
...

$ git add awesomefile.txt
$ git commit -m "Short awesome commit message"

TODO: Working With a Git Repository

View the diff between the two.

$ git diff master
diff --git a/awesomefile.txt b/awesomefile.txt
new file mode 100644
index 0000000..08cec7f
--- /dev/null
+++ b/awesomefile.txt
@@ -0,0 +1 @@
+Some awesome text

TODO: Working With a Git Repository

Locally merge the changes from your new branch into Master.

$ git checkout master
$ git merge add-awesome-feature
Updating 459de26..5c4ca48
Fast-forward
awesomefile.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 awesomefile.txt

What not to do with Git

Workflow(s)

Everybody uses VCS differently. Choose the workflow that works best for everybody involved.

git-flow diagram

Centralizing Git

Gitlab
Open Source, free to run, feature rich.
Github
Very popular. Not Open Source but free for Open Source projects.
Bitbucket
Also popular, similar to Github, unlimited free private and public repositories.
Gitolite
Bare-bones. Fewer features than the previous three. Open Source, useful for learning the nitty-gritty on how Git really works.

Cloning a Repository

To contribute to someone else's repository you first need to clone the repo.

$ cd /path/to/my/projects
$ git clone <some git url>
$ cd <new repo directory>
$ ls

Once you clone a repository you can make as many local changes as you want without affecting the original (central) copy. You can experiment and work without the original owner even knowing what you're doing!

TODO: Cloning Exercise

$ cd ~
$ git clone https://github.com/DevOpsBootcamp/tinsy-flask-app.git
$ cd tinsy-flask-app

See http://git.io/vcVmB for more details about the tinsy-flask-app repository.

TODO: Cloning Exercise

#Setup python virtual environment
$ virtualenv venv
$ source venv/bin/activate
(venv) $ pip install -r requirements.txt
#Run server
(venv) $ python script.py
#When finished, deactivate virtual environment
(venv) $ deactivate
$

Now if you go to dobc-shell.osuosl.org:<http port> in your web-browser to see a live version of the app!

Your <http port> is the same as your SSH port, but the first 2 numbers are changed from 33 to 34. For example, 33005 -> 34005

Further Reading

The Online Git Docs
This is a portal to all of the official docs on git-scm.com. It includes everything from Getting Started to Git Internals. Check it out!
Git workflow tutorial
This is the tutorial provided on https://git-scm.com/about/distributed. It is a good high-level overview of some common git workflows.
A successful Git branching model
This blogpost describes a git workflow (git-flow) that the Open Source Lab bases their workflow on.