diff --git a/content/git/concepts/log/log.md b/content/git/concepts/log/log.md index 9b173e63f3b..1b97bf47d67 100644 --- a/content/git/concepts/log/log.md +++ b/content/git/concepts/log/log.md @@ -12,20 +12,134 @@ CatalogContent: - 'learn-git' --- -In Git, the **`log`** command is used to track a [branch's](https://www.codecademy.com/resources/docs/git/branch) commit history. After running, it will display project-related information, such as a detailed commit history, which can be used as a reference to go back in time to a previous version of the project. +In Git, the **`log`** command is used to explore the timeline of [commits](https://www.codecademy.com/resources/docs/git/commit). After creating several commits or cloning a repository with an existing commit history, `git log` displays information about commits from the repository history in chronological order (the most recent commits show up first). -## Example +Each commit entry in a standalone `git log` output includes: + +- Commit ID: A unique [SHA-1](https://en.wikipedia.org/wiki/SHA-1) or hash ID that identifies the commit. +- Author: The name and email address of the commit author. +- Date: The date the snapshot was taken. +- Commit message: A description of the changes included in the commit. + +## Syntax -Below is a short example of how the `git log` command works. Running `git log` will show a list of all previous commits on the current branch: +```pseudo +git log +``` + +## Example ```shell -$ git log +## Sample git log entry -On current branch +$ git log commit f5b5bd8f9eaa443d4020cbe918x742e7ddd22000 -Author : John Doe -Date: Mon May 22 14:21:03 2023 -0400. +Author: Big-Logic +Date: Wed Apr 23 22:12:47 2025 +0530 + +Commit message tittle + + * Commit message description + + * Commit message description +``` + +The first entry in a `git log` output is the commit at which the active [branch](https://www.codecademy.com/resources/docs/git/branch) is currently pointed. + +## Options + +`git log` provides numerous options that are used for formatting, filtering, limiting, and sorting output. For example, the `--oneline` option displays each log entry as a single line of abbreviated commit hash and message. + +### Syntax + +```pseudo +git log --option +``` + +### Example + +```shell +$ git log --oneline + +3fe31700 (HEAD -> git-log) Refine git log documentation for clarity +b86d023d (upstream/main, origin/main, origin/HEAD, main) [Edit] Python .zip() (#6619) +cd781f26 [Edit] Python strip (#6617) +2394f728 [Edit] Python math.floor() (#6616) +423acf79 [Term Entry]: Add Five Whys (#6537) ``` -The example log above shows the different elements that make up a commit, but the most useful is the git hash (f5b5bd8f9eaa443d4020cbe918x742e7ddd22000), which can be used to revert our commit changes using the git revert command. +## Useful options + +### Formatting + +| Option | Description | +| ----------- | -------------------------------------------------------------------------------- | +| `--oneline` | Displays each log entry as a single line of abbreviated commit hash and message. | +| `--graph` | Displays an ASCII graph (text based diagram) beside each log entry. | +| `--stat` | Shows statistics for files modified in each commit. | +| `-p` | Shows the full details of the changes made in each commit, also known as patch. | + +### Filtering + +| Option | Description | +| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | +| `HEAD` | Shows the commit history of the current or active branch. | +| `` | Starts the log from the specified ``(usually the first seven characters of the commit hash) and shows all commits leading up to it. | +| `` | Shows the commit history of the specified branch. | +| `` | Shows only commits that changed the specified file or folder. | +| `--before=` | Shows commits made before the specified date. | +| `--after=` | Shows commits made after the specified date. | +| `--committer=` | Shows commits made by a committer matching the pattern. | +| `--author=` | Shows commits authored by contributor matching the pattern. | + +### Sorting + +| Option | Description | +| ----------- | ---------------------------------------------------------- | +| `--reverse` | Reverses the order of commits (oldest commit shown first). | + +### Limiting + +| Option | Description | +| -------------------------- | -------------------------------------------------------------------- | +| `-n `, `-` | Limits the output to the most recent `` of commits. | +| `--skip=` | Skips the first `` commits and then starts showing the rest. | + +## Options combination + +Options in `git log` can be combined to better customize the information shown and how they are displayed. For example, using `--graph` with `--oneline` displays each log entry as an abbreviated commit hash and message while displaying an ASCII graph beside each log. + +### Syntax + +```pseudo +git log --option --option +``` + +### Example + +```shell +$ git log --graph --oneline + +| * bd3b2cc (origin/cs1102-unit3) Add flowchart +* | 072ecae Merge pull request #7 from Big-Logic/cs1102-unit3 +|\| +| * 62b2b5a Add search functionality +| * 8c5a0bd Enhance student update functionality +``` + +## Navigating git log + +By default, `git log` displays logs in a command line paper program called [less](). + +### Useful navigation keys + +| Key | Command | +| ---------- | ---------------------------- | +| `j` | Move down one line | +| `k` | Move up one line | +| `Spacebar` | Move down one page | +| `b` | Move up one page | +| `g` | Go to the top of the page | +| `G` | Go to the bottom of the page | +| `q` | Exit the log |