# Inspecting History

> How to use `git log` to travel back in time and understand what happened, when, and by whom.

Subject: Version Control | Lesson 6 of 29 | 4 min read
Interactive lesson: https://codewithanimation.in/git/git-log
Previous: [Committing Changes](https://codewithanimation.in/git/git-commit.md) | Next: [Ignoring Files](https://codewithanimation.in/git/gitignore.md)

## git log

`git log` is the command you use to view the commit history of your repository. It prints out a chronological list of every snapshot ever taken, starting with the most recent.

By default, it shows the commit hash, the author, the date, and the commit message.

## Why it matters

If you join a new project, `git log` is how you read the story of the codebase. You can see who wrote what code, when features were introduced, and trace back exactly when a bug first appeared.

Without it, you just have a folder of files with no context.

## Formatting the output

The default `git log` output is very verbose and can be hard to read on large projects. Git provides powerful flags to format this output.

The most common is `git log --oneline`, which condenses every commit into a single line showing only the first 7 characters of the hash and the commit message.

You can also use `--graph` to draw a text-based representation of branching and merging on the left side of the terminal.

## Common mistake: Getting stuck in the pager

The #1 mistake beginners make is running `git log` in a large repository, seeing a colon `:` at the bottom of the screen, and not knowing how to escape. Git pipes long output through a 'pager' program (like `less`). Simply press the `q` key to quit and return to your prompt.

## Tip: The ultimate log alias

Senior developers almost never type just `git log`. They use a heavily formatted alias. A very popular one is `git log --graph --oneline --decorate --all`. This draws a beautiful, condensed, colored map of your entire project history.

## Try it: Read a repository history

Follow the commit graph and its branch labels, then replay the history reveal.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-log

## Check yourself

### Question 1

What is the primary purpose of `git log`?

- A. To view the history of commits in the repository.
- B. To show which files are currently modified and unstaged.
- C. To track error logs from your running web server.
- D. To undo the last commit.

Correct answer: A

`git log` prints the timeline of all snapshots (commits) that have been saved to the repository.

- Why not B: That is the purpose of `git status`.
- Why not C: Git is for version control, not application monitoring.
- Why not D: You would use `git revert` or `git reset` for that.

### Question 2

If you are stuck viewing a very long `git log` and see a `:` at the bottom of your terminal, how do you exit?

- A. Press Escape (Esc)
- B. Type 'exit' and press Enter
- C. Press the 'q' key
- D. Close the terminal window completely

Correct answer: C

Git uses a pager (usually `less`) to display long outputs. Pressing 'q' (for quit) returns you to the standard command prompt.

- Why not A: Escape won't exit the pager.
- Why not B: You can't type full commands into the pager.
- Why not D: While this works, it's unnecessary. Just press 'q'.

### Question 3

What does the `--oneline` flag do when added to `git log`?

- A. It only shows the very last commit.
- B. It formats each commit to take up exactly one line of text, showing a short hash and the message.
- C. It removes all line breaks from the commit messages.
- D. It prints the log continuously on a single scrolling line.

Correct answer: B

`git log --oneline` is the most common way to get a quick, dense overview of recent history without being overwhelmed by dates, authors, and long descriptions.

- Why not A: To show only the last commit, you would use `-n 1`.
- Why not C: It truncates the output to just the first line of the message.
- Why not D: That would be unreadable.

## Remember this

- `git log` shows your commit history.
- Press `q` to quit the log viewer.
- Use `--oneline` for a condensed view.
