How to Search in Git History or Git logs

Git provides a command-line interface (CLI) that allows users to interact with the repository and perform various operations. Here are some commonly used Git command-line commands “git log” used to analyze the logs or the history:

How to use git log (for git history)?


The git log command is commonly used to view the commit history in a Git repository.

Here’s an example of how to use the git log command:

git log

This command will display the commit history in reverse chronological order, starting with the most recent commit. Each commit will be shown with its unique identifier (SHA-1 hash), author information, date and time of the commit, and the commit message.

Example

$ git log
commit fa5d48061e276b31cf8944a840ac0945854550f3 (HEAD -> main)
Author: Your Name <[email protected]>
Date:   Fri Jun 9 18:34:00 2023 +0530

    text update

commit 76d8881f34ed89062b984e640544acf7429d56dc (origin/main, origin/HEAD)
Author: codetryout <[email protected]>
Date:   Sun May 21 13:19:24 2023 +0530

    Update index.html

How to get git log with single line outputs (minimum info)

By default, git log shows a compact view of the commit history. If you prefer a more condensed output, you can use the --oneline option:

git log --oneline

This will display each commit on a single line, showing only the commit identifier and the commit message.

Example

$ git log --oneline
fa5d480 (HEAD -> main) text update
76d8881 (origin/main, origin/HEAD) Update index.html
7fab49c Create CNAME
ed1a54c Delete CNAME
5c7f094 Update error-page.html
2b79441 Update CNAME
4498358 Create error-page.html
77bac1c Update README.md
de16a62 Update README.md
65a637d Create my-file.txt
83d3650 adding favicon

Search the entire Git history for a string change

The git log -S command allows you to search for changes in the Git repository that involve a specific string or pattern within the content of the changes. The command comes in handy for searching the entire history.

git log -S Your-String

Replace “Your-String” with the actual term you want to search for. This command will display a list of commits that introduced or removed the specified string or pattern. Each commit will be shown with its details, including the commit identifier, author, date, and commit message.

The -S option stands for “pickaxe” and is used for finding changes that introduced or removed the exact string or pattern provided.

Git history, search for a pattern in the entire content

Note that the -S option searches for changes that involve the specified string/pattern, rather than searching the entire content of the repository. If you want to search the entire content, including file names and commit messages, you may want to consider using other search methods, such as:

git log --all --grep= or git grep

These are just a few commonly used options for git log. There are many more options and combinations available to customize the output and filter the commit history based on your requirements. You can refer to the Git documentation for more detailed information on the available options and usage of git log.