In a Git code repository I want to list all commits that contain a certain word. I tried this
git log -p | grep --context=4 "word"
but it does not necessarily give me back the filename (unless it's less that 5 lines away from the word I searched for. I also tried
git grep "word"
but it gives me only present files and not the history.
How do I search the entire history so I can follow changes on a particular word? I intend to search my codebase for occurrences of word to track down changes (search in files history).
One more way/syntax to do it is: git log -S "word"
Like this you can search for example git log -S "with whitespaces and stuff @/#ΓΌ !"
git log
's pickaxe will find commits with changes including "word" with git log -Sword
If you want search for sensitive data in order to remove it from your git history (which is the reason why I landed here), there are tools for that. Github as a dedicated help page for that issue.
Here is the gist of the article:
The BFG Repo-Cleaner is a faster, simpler alternative to git filter-branch for removing unwanted data. For example, to remove your file with sensitive data and leave your latest commit untouched), run:
bfg --delete-files YOUR-FILE-WITH-SENSITIVE-DATA
To replace all text listed in passwords.txt wherever it can be found in your repository's history, run:
bfg --replace-text passwords.txt
See the BFG Repo-Cleaner's documentation for full usage and download instructions.
To use boolean connector on regular expression:
git log --grep '[0-9]*\|[a-z]*'
This regular expression search for regular expression [0-9]* or [a-z]* on commit messages.
After a lot of experimentation, I can recommend the following, which shows commits that introduce or remove lines containing a given regexp, and displays the text changes in each, with colours showing words added and removed.
git log --pickaxe-regex -p --color-words -S "<regexp to search for>"
Takes a while to run though... ;-)
vim-fugitive is versatile for that kind of examining in Vim.
Use :Ggrep
to do that. For more information you can install vim-fugitive and look up the turorial by :help Grep
. And this episode: exploring-the-history-of-a-git-repository will guide you to do all that.
You can try the following command:
git log --patch --color=always | less +/searching_string
or using grep
in the following way:
git rev-list --all | GIT_PAGER=cat xargs git grep 'search_string'
Run this command in the parent directory where you would like to search.
Source: Stackoverflow.com