Version Control with Git

What is version control?

do you need git

Why do I care about the history of a project?

final_doc

Configuration

In [ ]:
git config --global user.name "Andreas Mueller"
git config --global user.email "amueller@nyu.edu"
git config --global color.ui "auto"
git config --global core.editor "nano"
  • 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. Show your configuration:

In [ ]:
git config --list

Wolfman and Dracula

dracula and wolfman

Creating a repository

  • make a directory called planets
  • cd into planets directory
In [ ]:
git init
  • make planets directory a repository
In [ ]:
ls -a
  • .git directory is where git stores the history of the project
In [ ]:
git status

Tracking Changes to Files

  • Create a file to track
    • Open your text editor
    • Type: cold and dry but everything is my favorite color
    • save file as mars.txt
  • Check in with git

  • start tracking file

  • Record current state

    • -m : commit message inline

Exercise 1:

  • Create a file called jupiter.txt with a sentence about jupiter in your planets directory
  • start tracking and record the current state of jupiter.txt
  • Put your green post-it up when you are done
In [ ]:
git status
git add filename
git commit -m "commit message"

Viewing your history

In [ ]:
git log

Changing a File

Open your mars.txt file and add:

The two moons may be a problem for Wolfman

Check the status of your files

In [ ]:
git status

View the changes you made

In [ ]:
git diff mars.txt

Tell git which files you want to record changes in

In [ ]:
git add mars.txt

Save changes to revision history

In [ ]:
git commit -m "concerns about Mars' moons"

What are we doing?

git staging area

Exercise

  • Open file jupiter.txt in your text editor
  • Add a line
  • View the change you made using git
  • Record your changes in the project's history
  • View your project's history
  • Put up your green post-it
  • 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

In [ ]:
git diff

Changes between staging area and last commit

In [ ]:
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

In [ ]:
git diff HEAD~1

Changes made in the last 2 commits

In [ ]:
git diff HEAD~2

Changes made since commit hash...

In [ ]:
git diff 0b0d55e
  • first 7 characters
  • use git log to find commit you want

Recovering Older Versions

  • Overwrite mars.txt:
In [ ]:
echo 'The mummy will like the dry air.' >  mars.txt
cat mars.txt

Recover last recorded version:

In [ ]:
git checkout HEAD mars.txt
  • checkout HEAD means 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.
  • if you become "headless" do git checkout master

What is going on here

git checkout

Exercise

  • Overwrite and recover jupiter.txt
  • Put up your green post-it
  • Bonus:
    • revert to the first saved version of jupiter.txt
    • switch back to the most recent version of jupiter.txt