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

Configuration

  • 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 user.name "Your Name"
git config --global user.email "YourEmail@email.edu"
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 README.md
  • Check in with git
  •  
  • start tracking file
  •  
  • Record current state
  •  
    • -m : commit message inline
git status
git add README.md
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 hello_world.py that prints "hello world" when you run it
  • start tracking and record the current state of hello_world.py
  • 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 README.md 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 README.md
git add README.md
git commit -m "adding description of code to readme"

What are we doing?

Exercise 2

  • Open file README.md 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 README.md:
    •  
    •  
  • 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' > README.md
cat README.md
git checkout HEAD README.md

What is going on here:

Exercise 3:

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

local_version_control_w_readme

By abostroem

local_version_control_w_readme

  • 1,408