Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: grep file in all previous versions

Consider a Python configuration file with the following line:

THEME = 'gum'

The THEME key appears only once, so when I want to know what the THEME is, I grep the file:

grep THEME pelicanconf.py

The file is kept in git, and I would like to grep for THEME in all previous git commits, in order to tell when was this line changed.

Is there an elegant way to grep the entire history of a git file?

like image 834
Adam Matan Avatar asked Jan 03 '16 12:01

Adam Matan


People also ask

What files are searchable by git grep?

Git ships with a command called grep that allows you to easily search through any committed tree, the working directory, or even the index for a string or regular expression.

How do I see my entire git history?

On GitHub, you can see the commit history of a repository by: Navigating directly to the commits page of a repository. Clicking on a file, then clicking History, to get to the commit history for a specific file.

How does git grep work?

`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.

Which statement is the best comparison between git grep and grep?

The git grep version will only search in files tracked by git, whereas the grep version will search everything in the directory.


1 Answers

git log -S'THEME' -- pelicanconf.py | xargs -n 1 git show shows the content of every commit that changed THEME

However, it prints out full commits, not only changes.

var="THEME"; git log -S"$var" -p -- pelicanconf.py | egrep "$var|commit|Date:"

would show you all variants of THEME with commit hashes and dates.

Thx @knittl for -p option.

Also, found a solution with gitk: see here.

like image 151
John_West Avatar answered Oct 17 '22 11:10

John_West