6

I would like to order results returned by git grep using some git-related criteria, for example the commit date.

Is there a way to do that?

8
  • 1
    AFAIK git grep searches the work tree, not the entire history. Commented Dec 7, 2017 at 11:52
  • 1
    Check here git-scm.com/docs/git-log, there is Commit Ordering section and many other things you could do. Hope that helps Commented Dec 7, 2017 at 11:52
  • @GolfWolf: Yes, that is true - git grep searches the current work tree, but that would be sufficient for me. Commented Dec 7, 2017 at 12:10
  • Use tools like sort or awk on the results of git grep and sort them on your own Commented Dec 7, 2017 at 12:18
  • 2
    Try for commit in $(git log -S <pattern> --pretty=%H);do git grep <pattern> $commit;done. Extra options could be added to git log and git grep to limit and format the output. Commented Dec 7, 2017 at 12:33

1 Answer 1

0

Since you want to search the current worktree only, git log might be overkill. I haven't found a way to get git grep or something similar to output revision information, so it gets a bit complicated.

The snippet below greps for "test" in the current directory (subtree) and prints for each match:

  • Location (File:Line)

  • Commit hash

  • Date (committer)

  • Name (committer)

  • Commit Message

sort by file

word=test
git grep -nI "$word" | \
awk -F: '{printf $1" "$2" "; system("git blame \""$1"\" \"-L"$2","$2"\"")}' | \
awk '$3{print $1,$2,$3}' | \
while read -r file line hash; do \
  echo -en "$file:$line  " ; \
  git show -s '--format=%h  %ad  %an  %s' '--date=format:%Y-%m-%d_%H:%M:%S' "$hash" ; \
done | sort -V

sort by date

| sort -sk3r

show as table

git grep -nI "$word" | awk -F: '{printf $1" "$2" "; system("git blame \""$1"\" \"-L"$2","$2"\"")}' | awk '$3{print $1,$2,$3}' | while read -r file line hash; do   echo -en "$file:$line\t" ;   git show -s $'--format=%h\t%ad\t%cn\t%s' '--date=format:%Y-%m-%d_%H:%M:%S' "$hash" ; done | sort -V | sort -sk3r | column -ts $'\t' -o ' | '

This prints all matches of $word ("python" in this case) in the current directory together with commit data, sorted by date (newest first). Output:

$ git grep -nI python | awk ...
ceval_gil.c:1223       | 1ddfe593200 | 2025-06-20_04:23:38 | GitHub | msg 1 
remote_debug.h:905     | 0909d6d8e89 | 2025-05-26_15:31:47 | GitHub | msg 2
remote_debug.h:917     | 0909d6d8e89 | 2025-05-26_15:31:47 | GitHub | msg 2
optimizer_symbols.c:21 | ec736e7daec | 2025-05-22_11:15:03 | GitHub | msg 3
crossinterp.c:14       | c81fa2b9cd1 | 2025-05-08_09:07:46 | GitHub | msg 4
initconfig.c:4576      | ac5424d6a9f | 2025-04-25_18:30:39 | GitHub | msg 5
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.