Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search all of Git history for a string [duplicate]

Tags:

git

I have a code base which I want to push to GitHub as open source. In this Git-controlled source tree, I have certain configuration files which contain passwords. I made sure not to track this file and I also added it to the .gitignore file. However, I want to be absolutely positive that no sensitive information is going to be pushed, perhaps if something slipped in-between commits or something. I doubt I was careless enough to do this, but I want to be positive.

Is there a way to "grep" all of Git? I know that sounds weird, but by "all" I mean every version of every file that ever existed. I guess if there is a command that dumps the diff file for every commit, that might work?

like image 441
Jorge Israel Peña Avatar asked Dec 17 '10 06:12

Jorge Israel Peña


People also ask

How do I search text history in git?

Line Log Search Simply run git log with the -L option, and it will show you the history of a function or line of code in your codebase.

How do I search for a string in git log?

Git Browsing the history Searching commit string in git logWill search for removed file string in all logs in all branches. Starting from git 2.4+, the search can be inverted using the --invert-grep option.

What is git grep?

`git grep` command is used to search in the checkout branch and local files. But if the user is searching the content in one branch, but the content is stored in another branch of the repository, then he/she will not get the searching output.


2 Answers

Git can search diffs with the -S option (it's called pickaxe in the docs)

git log -S password 

This will find any commit that added or removed the string password. Here a few options:

  • -p: will show the diffs. If you provide a file (-p file), it will generate a patch for you.
  • -G: looks for differences whose added or removed line matches the given regexp, as opposed to -S, which "looks for differences that introduce or remove an instance of string".
  • --all: searches over all branches and tags; alternatively, use --branches[=<pattern>] or --tags[=<pattern>]
like image 162
Nathan Kinsinger Avatar answered Oct 25 '22 17:10

Nathan Kinsinger


git rev-list --all | (     while read revision; do         git grep -F 'password' $revision     done ) 
like image 21
cdhowie Avatar answered Oct 25 '22 17:10

cdhowie