Continuing last monthβs theme of searching Git history β here we look at how to find the source of changes, even after other people have worked on (or reformatted) the area in question.
The question: which commit actually introduced a given string into the file?
The βgit logβ command can help us here. To search for βsearchStringβ in a given file:
git log --source -S 'searchString' -- path/to/my/File.java
The same search throughout all files in the repository:
git log --source -S 'searchString' --all
As well as -S for simple search, you can use -G for regex; this is also more accurate as it detects lines moved in the file (-S doesnβt).
git log --source -G 'search\w+String' -- path/to/my/File.java
Use -p to view the diff of each change.
git log --source -p -G 'search\w+String' -- path/to/my/File.java
References:
β Stack Overflow: Git search for string in a single files history
β Stack Overflow: Finding a Git commit that introduced a string in any branch
β Git Tip of the Week: Searching for Commits and Changes
β βgit logβ documentation