Version Control with Git

What is version control?

  • A tool to track the history of a project
    • who changed what when
  • Individual or collaborative

Why do I can about the history of a project?

  • Individual:
    • fix (or undo) mistakes
    • what was done and why
    • definitive (final?) version
    • hard to accidentally delete
  • Collaborative:
    • who to ask if you have questions
    • work simultaneously
    • hard to overwrite


  • git command format: git verb​
  • verb: config
    • who you are
  • --global 
    • apply everywhere
  • color: color code output
  • editor: set default editor 
  • you can change these setting at any time
git config --global "Your Name"
git config --global ""
git config --global color.ui "auto"
git config --global core.editor "nano"

Creating a repository

  • cd to your home directory
  • make a directory called toy_repo
  • cd into toy_repo directory
git init
  • make toy_repo directory a repository 
ls -a
  • .git directory is where git stores the history of the project
git status

Tracking Changes to Files

  • Create a file to track
    • Open your text editor
    • Type: # My Toy Git Repo
    • save file as
  • Check in with git
  • start tracking file
  • Record current state
    • -m : commit message inline
git status
git add
git commit -m "starting project with readme"

What are we doing?

Exercise 1:

  • make a directory called code
  • cd into that directory
  • create a file called that prints "hello world" when you run it
  • start tracking and record the current state of
  • cd back to your toy_repo directory
  • Click your "yes" button when you are done
git status
git add filename
git commit -m "commit message"

Viewing your history

git log

Changing a File

  • Open your file and add:
    • This code prints hello world to the screen
  • Check the status of your files
  • View the changes you made
  • Tell git which files you want to record changes in
  • Save changes to revision history
git status
git diff
git add
git commit -m "adding description of code to readme"

What are we doing?

Exercise 2

  • Open file in your text editor
  • Add a line that says your are the author
  • View the change you made using git
  • Record your changes in the project's history
  • View your project's history
  • Click your "yes" button when you are finished
  • Bonus:
    • make another change
    • try any one of these variations:
      • commit without adding
      • view changes between adding and committing
      • commit without the -m

A note on viewing changes

    • changes between working directory and what was last staged
    • changes between staging area and last commit
git diff
git diff --staged

Referencing different versions

  • Shorthand for different versions of a repository (refers to commits)
    • Current Version (most recent commit): HEAD
    • Version before current: HEAD~1
    • Version before that: HEAD~2
  • Each of these also has a commit hash
    • use git log to get appropriate hash


Exploring History

  • Changes made in the last commit
  • Changes made in the last 2 commits
  • Changes made in the last 3 commits
  • Changes made since commit hash...
    • ​first 7 characters
    • use git log to find commit you want
git diff HEAD~1
git diff HEAD~2
git diff HEAD~3
git diff 0b0d55e

Recovering Older Versions

  • Overwrite
  • Recover last recorded version:
    • checkout HEAD = revert to version in HEAD
    • can use commit hash to revert to even older version
    • mars.txt: tells git which file to revert
    • in git status they list this option with -- instead of HEAD. This is a shortcut.
echo 'this project is stupid' >
git checkout HEAD

What is going on here:

Exercise 3:

  • Overwrite and recover
  • Click your 'yes' button when you are finished
  • Bonus:
    • revert to the first saved version of
    • switch back to the most recent version of


By abostroem


  • 764