乐闻世界logo
搜索文章和话题

How to grep search through committed code in the git history

4个答案

1
2
3
4
  1. Using git grep to Search the Working Directory:

If you only want to search files in the current working directory, you can directly use the git grep command. For example:

shell
git grep 'search keyword'
  1. Searching Content in Historical Commits:

To search for content in historical commits, combine the -p parameter of git log with the grep command. For example, to search for commits containing 'search keyword':

shell
git log -p -S'search keyword'

Here, -p displays the diff for each commit (i.e., code changes), and -S specifies the string to search for. -S identifies commits that added or removed the specified string.

  1. Combining git log with External grep Command:

You can pipe the output of git log to an external grep command to leverage its advanced search capabilities. For example:

shell
git log -p | grep 'search keyword'

For more specific information, such as displaying commit hashes where the keyword matches, add additional parameters. For example:

shell
git log -p | grep -B 4 'search keyword'

Here, -B 4 shows the matching line along with the four preceding lines, which typically includes the commit message.

  1. Using git log -G for Regular Expression Search:

For complex searches, use the -G parameter followed by a regular expression:

shell
git log -p -G'regex'
  1. Limiting the Search Scope to Specific Files:

To search only specific file types or paths, specify file paths or patterns in the git grep command. For example, to search only .c files:

shell
git grep 'search keyword' -- '*.c'
  1. Searching Content in Specific Branches or Tags:

To search content in a specific branch or tag, specify the branch or tag name in the git grep command. For example, to search in the branch named 'feature-branch':

shell
git grep 'search keyword' feature-branch

By following these steps, you can flexibly search through Git commit history. Remember to replace 'search keyword' and 'regex' with your actual search terms.

2024年6月29日 12:07 回复

Git's code history can be searched for specific keywords or patterns using git grep combined with parameters from git log. Here are the basic steps to achieve this:

  1. Using git grep to search the working directory:

    If you're only searching within the current working directory, you can directly use the git grep command. For example:

    shell
    git grep 'search keyword'
  2. Searching content in historical commits:

    To search content in historical commits, combine the -p parameter of git log with the grep command. For example, to search for commits containing 'search keyword':

    shell
    git log -p -S'search keyword'

    The -p parameter displays the diff for each commit (i.e., code changes), while the -S parameter specifies the string to search for. -S identifies commits that added or removed the specified string.

  3. Combining git log with an external grep command:

    You can pipe the output of git log to an external grep command to leverage its advanced search capabilities. For example:

    shell
    git log -p | grep 'search keyword'

    For more specific details, such as displaying the commit hash where the keyword matches, add additional parameters. For example:

    shell
    git log -p | grep -B 4 'search keyword'

    Here, -B 4 shows four lines before the matching line, which typically includes the commit message.

  4. Using git log -G for regular expression searches:

    For complex searches, use the -G parameter with a regular expression:

    shell
    git log -p -G'regex'
  5. Limiting the search to specific file paths:

    To search only specific file types or paths, specify them in the git grep command. For example, to search only .c files:

    shell
    git grep 'search keyword' -- '*.c'
  6. Searching content in specific branches or tags:

    To search content within specific branches or tags, specify the branch or tag name in the git grep command. For example, to search in the branch named 'feature-branch':

    shell
    git grep 'search keyword' feature-branch

By following these steps, you can flexibly search for code in Git history. Remember to replace 'search keyword' and 'regex' with your actual search terms.

2024年6月29日 12:07 回复

git log is a more efficient way to search across all branches for text, especially when there are many matches, and you want to view the most recent (relevant) changes first.

shell
git log -p --all -S 'search string'
shell
git log -p --all -G 'match regular expression'

These log commands list commits that add or delete the given search string or regular expression, typically showing the most recent first. The -p option displays the relevant differences at the locations where the pattern is added or deleted, allowing you to view it in context.

After finding the relevant commit that adds the text you're searching for (e.g., 8beeff00d), find the branch that contains this commit:

shell
git branch -a --contains 8beeff00d
2024年6月29日 12:07 回复

To search for commit content (i.e., actual source code lines, not commit messages), you need to execute the following:

bash
git grep <regexp> $(git rev-list --all)

Alternatively, git rev-list --all | xargs git grep <expression> will work if you encounter the "too long argument list" error.

If you want to restrict the search to a specific subtree (e.g., "lib/util"), you need to pass it to the sub rev-list command and grep:

bash
git grep <regexp> $(git rev-list --all -- lib/util) -- lib/util

This will search through all commit texts for regexp.

The reason for passing the path in both commands is that rev-list returns the revision list where changes occurred for lib/util, but you also need to pass it to grep so it only searches within lib/util.

Imagine the following scenario: grep might find the same content on other files included in the returned revision (even if the file wasn't changed on that revision), due to the interaction between <regexp> and rev-list.

Here are some other useful methods for searching:

  • In the working tree, search for text matching regular expression regexp:
    bash

git grep

shell
- In the working tree, search for lines matching regular expression `regexp1` or `regexp2`: ```bash git grep -e <regexp1> [--or] -e <regexp2>
  • In the working tree, search for lines matching regular expression regexp1 and regexp2, only reporting file paths:
    bash

git grep -l -e --and -e

shell
- In the working tree, search for files that have lines matching regular expression `regexp1` and `regexp2`: ```bash git grep -l --all-match -e <regexp1> -e <regexp2>
  • In the working tree, search for lines matching the text pattern that have been changed:
    bash

git diff --unified=0 | grep

shell
- Search all revisions to find text matching regular expression `regexp`: ```bash git grep <regexp> $(git rev-list --all)
  • Search all revisions between rev1 and rev2 to find text matching regular expression regexp:
    bash

git grep $(git rev-list ..)

shell
2024年6月29日 12:07 回复

你的答案