Git History and Diff Checking Commands Guide

History and Diff Checking

git log - Display Commit History

Displays the commit history of the repository.

Option Description
--oneline Display each commit on a single line
--graph Display branch and merge history as an ASCII graph
--decorate Show references (branches, tags, etc.)
--all Show commits from all branches
-p, --patch Show the diff for each commit
-n <number> Limit the number of commits displayed
--author=<pattern> Show only commits by specific author
--since=<date>, --after=<date> Show commits more recent than specified date
--until=<date>, --before=<date> Show commits older than specified date

Examples:

git log - Display commit history
git log --oneline - Display commit history in a concise format
git log --oneline --graph --all - Display commit history for all branches in graph format
git log -p - Display commit history including diffs
git log -n 5 - Display only the 5 most recent commits
git log --author="John" - Display only commits by John
git log --since="2023-01-01" - Display commits since January 1, 2023
git log file.txt - Display commit history for a specific file

git diff - Display Changes

Displays changes between files.

Option/Argument Description
(no arguments) Show differences between working directory and staging area
--staged, --cached Show differences between staging area and the latest commit
<commit1> <commit2> Show differences between two commits
--name-only Show only names of changed files
--name-status Show names of changed files and their status (added/modified/deleted)
--color-words Highlight changes at word level

Examples:

git diff - Show differences between working directory and staging area
git diff --staged - Show differences between staging area and the latest commit
git diff HEAD - Show differences between working directory and the latest commit
git diff HEAD~1 HEAD - Show differences between the latest commit and the previous commit
git diff main feature - Show differences between main branch and feature branch
git diff -- file.txt - Show differences for a specific file only

How to Read Diff Output

Understanding the output of git diff commands is essential for accurately interpreting changes. Here's a guide to reading diff output.

Basic Structure of Diff Output

diff --git a/filename b/filename
index hash1..hash2 mode
--- a/filename
+++ b/filename
@@ -startline,count +startline,count @@ context info
- deleted line
+ added line
  unchanged line
            
  • diff --git a/filename b/filename: Shows the files being compared. 'a' is the original file, 'b' is the modified file.
  • index hash1..hash2 mode: Shows the object IDs and file mode.
  • --- a/filename: Indicates the original file.
  • +++ b/filename: Indicates the modified file.
  • @@ -startline,count +startline,count @@: Shows the location of the changes.
    • -startline,count: Starting line and number of lines in the original file
    • +startline,count: Starting line and number of lines in the modified file
  • Lines starting with -: Lines that were deleted.
  • Lines starting with +: Lines that were added.
  • Lines starting with a space: Unchanged lines (context).

Example of Diff Output

diff --git a/example.txt b/example.txt
index abc1234..def5678 100644
--- a/example.txt
+++ b/example.txt
@@ -1,5 +1,6 @@
 This is an unchanged line.
-This line was deleted.
+This line was added.
+This is another added line.
 This is also an unchanged line.
-This line was also deleted.
+This line was modified.
 This is the last unchanged line.
            

In this example:

  • Line 1 is unchanged.
  • Line 2 was deleted and replaced with a new line.
  • A new line was added at line 3.
  • Line 4 is unchanged.
  • Line 5 was deleted and replaced with modified content.
  • Line 6 is unchanged.