pamela.fox | Edit Slideshow | Copy Slideshow Download PDF | Sign out

Press key to advance. Zoom in/out: Ctrl or Command + +/-.

Version Control & Git

Version Control Systems

VCSs keep track of changes to files. Why do we use them?

  • History: Rolling back and Diffing
  • Collaboration: Sharing, Merging, and Blaming

Example Flow

Alice creates this code and commits it as version 0:

function addTwo(num) {
  return num + 2;
}

Alice edits and commits as version 1:

function addThree(num) {
  return num + 3;
}

Example Flow 2

Bob pulls the code from version 0 and edits:

function addTwo(num) {
  if (num == Infinity) {
    alert("Cant add to infinity!");
  }
  return num + 2;
}

Bob pulls in Alice's changes (version 1) and it merges them:

function addThree(num) {
  if (num == Infinity) {
    alert("Cant add to infinity!");
  }
  return num + 3;
}

Bob pushes the merged code (version 2).

VCS: Centralized vs. Distributed

Centralized Distributed
e.g. CVS, Perforce, SVN e.g. Mercurial, Git
Data stored on server Data stored locally
Each client keeps "workspace" of current state Each client keeps entire history
Server is required for each operation Server required only for collaboration

Git Workflow

Setting up the repo

Create a folder or navigate to existing folder:

cd myproject

Initialize a repository in the folder:

git init

All git info will be stored in .git folder.

Adding changes to staging

Git requires you to explicitly tell it what changes to stage (edits, deletions, and adds).

Add a new file to staging:

git add myfile.txt

Add a file's edits to staging:

vi myfile.txt
git add myfile.txt

Mark a file for deletion in staging:

git rm myfile.txt

Remove a file's changes from staging:

git reset myfile.txt

Viewing staged changes

Always review the changes in staging before committing using git status:

username@hostname: git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#    new file:   myfile.txt

Committing changes

To commit the changes to the local repo, use git commit:

username@hostname: git commit -m "First commit, first file."
[master (root-commit) 6853adc] First commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 myfile.txt

Try to have a descriptive message, you'll thank yourself later.

Viewing commits

Use git log to see all your commits:

username@hostname: git log
commit 6853adc0b6bc35f1a8ca0a6aa5e59c978148819b
Author: Pamela Fox 
Date:   Thu Jun 28 16:01:22 2012 -0700

    First commit

Each commit is associated with a unique hash.

Viewing old versions

Use git show to view old versions of files:

username@hostname: git show 6853adc0b6bc35f1a8ca0a6aa5e59c978148819b:myfile.txt
helllo!

Comparing versions

Use git diff to compare your changes to other versions:

username@hostname: vi myfile.txt
username@hostname: git add myfile.txt
username@hostname: git commit -m "Added second line"
username@hostname: git diff 6853adc0b6bc35f1a8ca0a6aa5e59c978148819b
diff --git a/myfile.txt b/myfile.txt
index f69d457..e8215de 100644
--- a/myfile.txt
+++ b/myfile.txt
@@ -1 +1,2 @@
 helllo!
+how are you?

Comparing versions

You can also use git diff to compare unstaged changes:

vi myfile.txt
git diff

Or to compare staged changes:

vi myfile.txt
git add myfile.txt
git diff --staged

Going back

Use git checkout to check out a past commit:

username@hostname: git checkout 6853adc0b6bc35f1a8ca0a6aa5e59c978148819b
HEAD is now at 6853adc... First commit

Other ways to refer to commits:

  • git checkout master
  • git checkout HEAD
  • git checkout HEAD^0, HEAD^1, HEAD^2,...

Best practice: commit or reset changes before checking out.

Using branches

Create the branch:

git branch mybranch

Switch to the branch:

git checkout mybranch

Or do it in one command:

git checkout -b mybranch

Merging branches

Switch to the branch you want to merge into:

git checkout master

Merge changes from other branch:

git merge mybranch

If there are any conflicts, fix and commit.

Optionally, delete the branch after:

git branch -d mybranch

Github

  • Hosted, shared git repositories
  • Code reviews
  • Issue tracking
  • Wiki
  • Strong community

Free for open-source, public projects. $$ for private projects.

Using remote repos

To synchronize with a remote repository, first tell Git about it:

git remote add origin git://github.com/username/reponame

To push changes, use git push:

git push origin master

To pull changes, use git pull:

git pull origin master