Version Control with Git

What is version control?

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

What can I do 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"

The Situation

  • Dracula, Wolfman, and the Mummy 
  • research moving to another planet
  • want to share their research

Creating a repository

  • make a directory called planets
  • cd into planets directory
git init
  • make planets 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: 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
git status
git add mars.txt
git commit -m "starting to think about mars"

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
git status
git add filename
git commit -m "commit message"

Viewing your history

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
    •  
  • View the changes you made
    •  
  • Tell git which files you want to record changes in
    •  
  • Save changes to revision history
    •  
git status
git diff mars.txt
git add mars.txt
git commit -m "concerns about Mar's moons"

What are we doing?

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
  •  
    • 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 mars.txt:
    •  
    •  
  • 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 'the mummy will like the dry air' >  mars.txt
cat mars.txt
git checkout HEAD mars.txt

What is going on here:

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

Ignoring Things

  • Add new files
    •  
  • Check status and see files need to be added
    •  
  • Create .gitignore
    •  
  • Add files to .gitignore
    •  
  • Check status again
touch a.dat b.dat c.dat
git status
touch .gitignore
echo "*.dat" >> .gitignore

Exercise:

  • Create a directory
  • Add several files to the directory
  • Add all the files to your .gitnore file and make sure they are ignored
  • Put up your green post-it
  • Bonus:
    • Ignore all the files by adding just 1 line .gitignore

deck

By abostroem

deck

  • 740
Loading comments...

More from abostroem