Bird
Raised Fist0
Gitdevops~5 mins

Viewing commit history with git log - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Sometimes you want to see what changes were made in your project over time. The git log command shows a list of past changes, called commits, so you can understand the history of your work.
When you want to check who made a change and when in your project
When you need to find a specific change to fix a bug
When you want to review the history before merging branches
When you want to see the messages describing past changes
When you want to track progress or understand the development timeline
Commands
This command shows a simple list of commits with their short IDs and messages, making it easy to scan the history quickly.
Terminal
git log --oneline
Expected OutputExpected
abc1234 Fix typo in README def5678 Add user login feature 789abcd Initial commit
--oneline - Shows each commit in one line with short ID and message
This command shows the detailed changes (diffs) for the last two commits, helping you see exactly what was added or removed.
Terminal
git log -p -2
Expected OutputExpected
commit abc1234 Author: Jane Doe <jane@example.com> Date: Tue Apr 23 10:00 2024 +0000 Fix typo in README diff --git a/README.md b/README.md index e69de29..d95f3ad 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -Hello Wrold +Hello World +Added greeting line commit def5678 Author: John Smith <john@example.com> Date: Mon Apr 22 09:30 2024 +0000 Add user login feature (diff output continues...)
-p - Shows the patch (diff) introduced in each commit
-2 - Limits output to the last two commits
This command shows a visual graph of all branches and commits with labels, making it easier to understand branching and merging.
Terminal
git log --graph --decorate --all --oneline
Expected OutputExpected
* abc1234 (HEAD -> main) Fix typo in README * def5678 (origin/main) Add user login feature * 789abcd Initial commit
--graph - Draws a text-based graph of the commit history
--decorate - Shows branch and tag names next to commits
--all - Shows commits from all branches
--oneline - Shows each commit in one line
Key Concept

If you remember nothing else from this pattern, remember: git log shows your project's history so you can understand what changed and when.

Common Mistakes
Running git log without any flags and getting too much information
The default output can be long and hard to read for beginners
Use flags like --oneline or -2 to limit and simplify the output
Expecting git log to show file contents instead of commit history
git log only shows commit metadata and messages, not file contents
Use git show or git diff to see file changes
Summary
Use git log to see the history of commits in your project.
Flags like --oneline and -p help simplify or detail the output.
The --graph and --decorate flags help visualize branches and labels.

Practice

(1/5)
1. What does the git log command do in a Git repository?
easy
A. Deletes the last commit from the repository
B. Shows the history of commits made in the repository
C. Creates a new branch in the repository
D. Displays the current status of files in the repository

Solution

  1. Step 1: Understand the purpose of git log

    The git log command is used to view the commit history in a Git repository.
  2. Step 2: Compare with other Git commands

    Other commands like git status show file status, and git branch manages branches, but git log specifically shows commits.
  3. Final Answer:

    Shows the history of commits made in the repository -> Option B
  4. Quick Check:

    git log = commit history [OK]
Hint: Remember: log means history or record [OK]
Common Mistakes:
  • Confusing git log with git status
  • Thinking git log deletes commits
  • Mixing git log with branch creation
2. Which of the following is the correct command to show a simplified one-line summary of each commit in Git?
easy
A. git log --oneline
B. git log --summary
C. git log --details
D. git log --short

Solution

  1. Step 1: Identify the option for one-line commit summary

    The --oneline option condenses each commit to a single line showing the commit hash and message.
  2. Step 2: Verify other options

    Options like --summary, --details, and --short are not valid git log options for this purpose.
  3. Final Answer:

    git log --oneline -> Option A
  4. Quick Check:

    --oneline = short commit list [OK]
Hint: Use --oneline for brief commit history [OK]
Common Mistakes:
  • Using non-existent options like --summary
  • Confusing --oneline with --short
  • Forgetting the double dash before options
3. Given the command git log -2 --oneline, what will be the output?
medium
A. Shows the last two commits in a detailed multi-line format
B. Shows all commits except the last two
C. Shows the last two commits each summarized in one line
D. Shows the first two commits in the repository

Solution

  1. Step 1: Understand the -2 option

    The -2 option limits the output to the last two commits only.
  2. Step 2: Understand the --oneline option

    The --oneline option shows each commit in a single line summary.
  3. Final Answer:

    Shows the last two commits each summarized in one line -> Option C
  4. Quick Check:

    -2 + --oneline = last two commits short [OK]
Hint: Combine -n with --oneline for short recent commits [OK]
Common Mistakes:
  • Thinking -2 shows first two commits
  • Expecting detailed multi-line output with --oneline
  • Confusing exclusion of commits with limiting output
4. You ran git log --oneline -p but got an error. What is the likely cause?
medium
A. The options --oneline and -p cannot be used together
B. You need to specify a branch name with the command
C. You must run git fetch before using git log
D. The repository has no commits yet

Solution

  1. Step 1: Understand the options --oneline and -p

    --oneline shows a brief summary, while -p shows patch (diff) details.
  2. Step 2: Check compatibility of options

    These two options conflict because one shows a short summary and the other shows detailed changes, so Git throws an error.
  3. Final Answer:

    The options --oneline and -p cannot be used together -> Option A
  4. Quick Check:

    --oneline + -p conflict = error [OK]
Hint: Avoid combining --oneline with -p; they conflict [OK]
Common Mistakes:
  • Assuming branch name is required for git log
  • Thinking git fetch fixes git log errors
  • Ignoring that empty repo causes no output, not error
5. You want to see the commit history with each commit showing the author, date, and a one-line message, all in a compact format. Which command should you use?
hard
A. git log --graph --decorate
B. git log --oneline --author-date-order
C. git log -p --stat
D. git log --pretty=format:"%h - %an, %ar : %s"

Solution

  1. Step 1: Understand the requirement for custom format

    You want author, date, and message in one line, so a custom format with --pretty=format: is needed.
  2. Step 2: Analyze the format string

    %h is short commit hash, %an is author name, %ar is relative date, and %s is commit message. This matches the requirement.
  3. Step 3: Check other options

    --oneline --author-date-order does not show author or date explicitly. -p --stat shows diffs and stats, not compact. --graph --decorate shows branch graph and refs, not author/date/message in one line.
  4. Final Answer:

    git log --pretty=format:"%h - %an, %ar : %s" -> Option D
  5. Quick Check:

    Custom format = author, date, message [OK]
Hint: Use --pretty=format for custom commit info display [OK]
Common Mistakes:
  • Using --oneline without author or date info
  • Expecting -p to show summary info
  • Confusing --graph with formatting output