When searching in Git diff for a term, can I only show the line that includes that term?

I'm doing a CSS cleanup and want to find where the class was used in our code to make sure it was actually used in our code and not in our CMS or the like.

We are using Git, so I use the following to find code differences that contain the search term I'm looking for, scrollerWrapper .

git log -GscrollerWrapper -p

      

With the flag, -p

I get the actual changes to the file, but in some cases it returns files thousands of lines long. Ideally, I would like to see only the lines (lines) that the term is in (even better if I could see the x lines before / after the line).

I checked the documentation for git -log , but I don't see the corresponding option.

Is it possible to pass in any additional parameter that would limit diff to only the lines containing my search term, or launch another query that would?

Thank!

+3


source to share


3 answers


The simplest is to use the search tools built into the pager , which allows the text to scroll git log

.



For most pager types /

follow whatever you want to find. So, run git log -GscrollerWrapper -p

as usual, then type /

, then scrollerWrapper

. This will lead to the first appearance scrollerWrapper

. Clicking n

will take you to the next one.

+1


source


You can pipe the output git log

to grep

. On Windows, you can do this with Git Bash. grep

has parameters indicating how much context to specify for each matched line.



0


source


Generally speaking, I always rely on the venerable Gitka whenever I need to search for a repository. Luckily, if you know your way around the command ps

, you can see the actual commands that Gitk is running in the background while you search. Gitk offers a search function that scans the repository history to add / remove a line (which I believe is what you are looking for). The team that it actually runs in the background is git diff-tree

, which is an extremely complex team with approximately 47 billion options.

Anyway, I think something like this git diff-tree -r -U3 -Ssearch_string <tree-ish search target>

will work for you.

  • -r

    does recursive search for the entire object
  • -U

    designed to output a unified diff, where 3

    is the number of lines of context
  • -S

    for a real search string. The format looks funny when you type it this way and don't forget that you may need to avoid special characters depending on what you are looking for
  • <tree-ish search target>

    where you want to search. You can simply put a hash fixing your current HEAD

    or master

    or any other tree ref.

If you've ever created unified differences from git on the command line before, the output should be pretty familiar here and should look something like this:

$ git diff-tree -r -U3 -Sexisting_user 599479fc43401942cedef7187677fa222ec54c94
599479fc43401942cedef7187677fa222ec54c94
diff --git a/src/AppBundle/Controller/SubscriptionController.php b/src/AppBundle/Controller/SubscriptionController.php
index 3670973..55de07a 100644
--- a/src/AppBundle/Controller/SubscriptionController.php
+++ b/src/AppBundle/Controller/SubscriptionController.php
@@ -23,28 +23,17 @@ class SubscriptionController extends AbstractController
         $form->handleRequest($request);

         if ($form->isSubmitted() && $form->isValid()) {
-            $existing_user = $this->get('user_dao')->getUserByEmail($newly_invited_user->getEmail());
...

      

This should get all you need to capture the ref, line number and context.

0


source







All Articles