Lesson 14 of 60 – Git Log
23%

Git Log

The git log command is used to view the commit history of a Git repository. It helps you see previous commits, commit messages, authors, dates, and commit identifiers.

Note: The git log command only displays information about commits that already exist in the repository.

1. What is git log?

git log is a Git command used to display the commit history of a repository.

git log

It shows information about commits in reverse chronological order, with the most recent commit normally appearing first.

2. Why Use git log?

The git log command is useful when you want to understand how a project has changed over time.

  • View previous commits
  • Find commit identifiers
  • Read commit messages
  • Check authors
  • Check commit dates
  • Understand project history

3. Basic Syntax

The basic syntax is:

git log

Run this command from inside a Git repository.

4. Example of git log

Suppose a project contains three commits. Running:

git log

may display information similar to:

commit 7a91bc2...
Author: Student
Date:   ...

    Add contact page

commit 3f82de1...
Author: Student
Date:   ...

    Add homepage

commit 1d72abc...
Author: Student
Date:   ...

    Initial commit

5. Commit Hash

Every commit has a unique identifier called a commit hash.

commit 7a91bc2...

The hash is used by Git to identify a particular commit.

6. Author Information

The log normally displays the author associated with each commit.

Author: Student <student@example.com>

Git gets this information from the user's Git configuration.

7. Commit Date

The commit history also displays date and time information associated with commits.

Date:   Tue Sep 24 12:00:00 2026 +0530

The exact format can vary depending on Git configuration and terminal.

8. Commit Message

The commit message describes the changes represented by the commit.

git commit -m "Add login page"

The message can later be seen in the output of git log.

9. Commit History Order

By default, git log displays the most recent commits first.

Latest Commit
      ↓
Previous Commit
      ↓
Older Commit
      ↓
Initial Commit

10. Compact Log with --oneline

The --oneline option provides a compact version of the commit history.

git log --oneline

This is useful when you want to quickly scan many commits.

11. Example of --oneline

7a91bc2 Add contact page
3f82de1 Add homepage
1d72abc Initial commit

Each line contains a shortened commit identifier followed by the commit message.

12. Viewing the Latest Commit

You can use:

git log -1

This limits the output to the latest commit.

13. Viewing Multiple Recent Commits

You can specify how many commits you want to display. For example:

git log -3

This displays the three most recent commits.

14. Using --oneline with a Limit

You can combine options:

git log --oneline -5

This shows a compact list of the five most recent commits.

15. Viewing Commit Details

The git show command can be used when you want more detailed information about a specific commit.

git show 7a91bc2

The commit identifier can be obtained from git log.

16. Viewing the Current Branch History

The normal git log command shows the history reachable from the current branch.

git log

This makes it useful for understanding the history of the branch you are currently working on.

17. Viewing All Branches

You can include histories from all local branches with:

git log --all

This can help when exploring a repository with multiple branches.

18. Graph View

The --graph option displays an ASCII graph showing relationships between commits.

git log --graph

This becomes especially useful when a project contains branches and merges.

19. Graph with One-Line Format

You can combine the graph and compact format:

git log --oneline --graph

This provides a compact visual representation of commit history.

20. Graph with All Branches

A commonly useful combination is:

git log --oneline --graph --all

This can display a compact graph containing commits reachable from all local references.

21. Searching Commit Messages

The --grep option can search commit messages.

git log --grep="login"

This searches for commits whose messages match the specified pattern.

22. Viewing Commits by Author

You can filter commits by author using:

git log --author="Student"

This can be useful in repositories with multiple contributors.

23. Viewing Changes with --stat

The --stat option provides a summary of file changes associated with commits.

git log --stat

It can show files changed and summarized line changes for commits.

24. Viewing Patch Information

The log command can also show patch information:

git log -p

This displays the changes introduced by commits in patch format.

25. Limiting Log by File

You can view commits related to a specific file:

git log -- index.html

The -- separates revision options from the file path.

26. Useful git log Commands

git log
git log --oneline
git log -5
git log --oneline -5
git log --all
git log --graph
git log --oneline --graph --all
git log --stat
git log -p

These options help you inspect project history in different ways.

27. Common git log Mistakes

  • Running git log outside a Git repository.
  • Expecting uncommitted changes to appear as commits.
  • Confusing a commit hash with a branch name.
  • Forgetting that normal log output can be long.
  • Not using --oneline when a compact history is preferred.
  • Confusing commit history with uncommitted working-directory changes.

28. Complete Git History Workflow

git status
git add .
git commit -m "Update project"

git log

git log --oneline

git log --oneline --graph --all

This workflow lets you create commits and then inspect the history in different formats.

29. Practical Example

git init

touch index.html

git add index.html
git commit -m "Create homepage"

touch about.html

git add about.html
git commit -m "Create about page"

git log --oneline

The final command displays the commit history in a compact format.

30. Summary of git log

The git log command is used to inspect the commit history of a Git repository.

git log

For a shorter history, use:

git log --oneline

For a visual branch history, use:

git log --oneline --graph --all

Understanding git log helps you track how a project has developed and locate important commits in its history.

📌 Key Points

  • git log displays commit history.
  • It shows commit identifiers, authors, dates and messages.
  • git log --oneline provides a compact history.
  • git log -5 shows the five most recent commits.
  • git log --all includes history from all local references.
  • git log --graph displays a commit graph.
  • git log --grep can search commit messages.
  • git log --author can filter commits by author.
  • git log --stat displays file-change statistics.
  • git log -p displays patch information.
  • git log -- filename shows commits related to a file.

🧠 Quick Quiz

Question: Which Git command is used to view the commit history?