Comparing versions

Introduction to Git

George Boorman

Curriculum Manager, DataCamp

git show output

git show output displaying the git log output at the top and the git diff output underneath, with the incorrect data entry at the bottom

Introduction to Git

git diff

  • git diff - Difference between versions

 

  • Compare last committed version with latest version not in the staging area
git diff report.md
Introduction to Git

Comparing to an unstaged file

git diff output showing "TODO: write executive summary."" was removed and "TODO: cite funding sources" was added

Introduction to Git

git diff output

git diff output annotated with an arrow pointing to the fifth line of the output containing "@@ -1, 5 +1, 5 @@", representing the start and end lines of the two versions

Introduction to Git

git diff output

git diff output annotated with an arrow pointing to the fifth line of the output containing "@@ -1, 5 +1, 5 @@", representing start and end lines of the two versions

Introduction to Git

git diff output

git diff output annotated showing one line was added at the bottom in green

Introduction to Git

Comparing to a staged file

  • Add report.md to the staging area
git add report.md
  • Compare last committed version of report.md with the version in the staging area
git diff --staged report.md
Introduction to Git

Comparing to a staged file

git diff --staged report.md output, matching the previous output for git diff report.md

Introduction to Git

Comparing multiple staged files

  • Compare all staged files to versions in the last commit
git diff --staged

git diff --staged output showing one line added to mh tech survey, plus one line removed and added to the report

Introduction to Git

Comparing two commits

  • Find the commit hashes

    git log
    
  • Compare them

    git diff 35f4b4d 186398f
    
  • What changed from first hash to second hash

    • Put most recent hash second
  • State in latest commit = HEAD
  • Compare second most recent with the most recent commit
    git diff HEAD~1 HEAD
    
Introduction to Git

Comparing two commits

git diff output for the first and third most recent commits - showing the latest version has an extra line

Introduction to Git

Summary

Command Function
git diff Show changes between all unstaged files and the latest commit
git diff report.md Show changes between an unstaged file and the latest commit
git diff --staged Show changes between all staged files and the latest commit
git diff --staged report.md Show changes between a staged file and the latest commit
git diff 35f4b4d 186398f Show changes between two commits using hashes
git diff HEAD~1 HEAD~2 Show changes between two commits using HEAD instead of commit hashes
Introduction to Git

Let's practice!

Introduction to Git

Preparing Video For Download...