32

Before to publish branch sometimes I want to search for a specific keyword to ensure I have fixed it correctly everywhere. The problem is, I want to search it only in changes not for a whole project.

Is it possible?

b00sted 'snail'
  • 354
  • 1
  • 13
  • 27
  • Can you specify, whether you want to make this work with the Git source control extension, or for any of them generically? If for Git, you could perhaps try to talk to the Git extension to find the list of changed files and their diffs, or you could use the Git command-line behind the scenes. If you want it to work for any version control, I do not think there is a way, because the SCM extension can represent the file groups in any custom way. – Jan Dolejsi Nov 15 '19 at 17:26
  • 3
    Please let us know if command line calls could be acceptable for you: I feel like this can be accomplished with those. – Daemon Painter Jun 17 '20 at 07:44

6 Answers6

29

As AsGoodAsItGets pointed out in one of the comments there is a feature request pending on VS Code github which will allow us to do that.

In the meantime I have a workaround:

  1. Close all open files
  2. Open Source Control

enter image description here

  1. Right click on Changes and choose Open changed files. Note that it will open also deleted files.

enter image description here

  1. Go to Search and select Search only in Open Editors

enter image description here

Not ideal, but that's how I search for my console.log leftovers ;)

Buszmen
  • 1,978
  • 18
  • 25
4
  1. Open the terminal by typing ^, using the View > Terminal menu option, or running the View: Toggle Integrated Terminal command.
  2. Run git diff BRANCH1..BRANCH2 -G REGEX. Replace BRANCH1 with the branch to compare against, BRANCH2 with the current branch, and REGEX with a regular expression of the keyword(s) you're searching.

If you want to see the results in a VSCode editor rather than in the intergrated terminal, append | code - to the git diff command.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
Nick McCurdy
  • 17,658
  • 5
  • 50
  • 82
2

Refer these screenshots. You can search in Open files easily.enter image description here

enter image description here

Also you can use File to include feature

enter image description here enter image description here

If you want to find all historical commits where commit message contains given word, use

$ git log --grep=word

If you want to find all commits where "word" was added or removed in the file contents (to be more exact: where number of occurences of "word" changed), i.e. search the commit contents, use so called 'pickaxe' search with

$ git log -Sword

In modern git there is also

$ git log -Gword

to look for differences whose added or removed line matches "word" (also commit contents).

Note that -G by default accepts a regex, while -S accepts a string, but can be modified to accept regexes using the --pickaxe-regex.

Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33
  • 2
    How do you get the search dialog in your first screenshot in vscode? The one that includes the "search in" label/input? I can only get the one with "files to include" and "files to exclude". – AsGoodAsItGets Feb 17 '21 at 10:22
  • 6
    I just noticed that this is not yet implemented, so I assume you got that screenshot from here? https://github.com/microsoft/vscode/issues/20530 – AsGoodAsItGets Feb 17 '21 at 10:24
  • 2
    Please. That's a user-made implementation to illustrate the issue. – 丶 Limeー来夢 丶 Mar 28 '22 at 01:27
  • 1
    @Kunal Vohra. Can you update/clarify your post? The feature you mention (in the images) is not implemented in VSCode (yet). – schuhwerk Apr 20 '22 at 11:01
1

From the command line :

  1. git grep "pattern" will look for a pattern in tracked files.

Note that it will search the complete content of searched files, not just the diff part (that would be git diff -G or git log -G, as @NickMcCurdy and @KunalVohra suggested).

If you specify a commit reference, it will look into the version stored in that commit, e.g :

# this will look into the files as they are on disk (working tree) :
git grep "pattern"

# this will look into files as stored in the last commit :
git grep "pattern" HEAD
  1. git diff --name-only will list the files modified between the two target commits

By combining the two :

git grep "pattern" HEAD -- $(git diff --name-only HEAD^ HEAD)

you can search for pattern, looking only in files mmodified by the last commit.


You can add -i for a case insensitive search : git grep -i "pattern" ;

if you want to look into the files changed since commit eacf32 :

# change the file listing part to :
$(git diff --name-only eacf32 HEAD)

if you want to search the files that "were modified since the branch forked off master" :

$(git diff --name-only master...HEAD)  # that's 3 dots

I'm not aware of a vscode extension that allows you to run git grep, you can run the above commands from a bash terminal opened in VSCode.

As @NickMcCurdy suggested : you can add | code - at the end of this command to open the result in a vscode editor.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
1

You can't.

Two options:

  1. Suggest this as an feature on VSCode repository.

  2. Use the following command in a terminal to search for a string within all the staged files.

    git grep --cached "myString" $(git diff --cached --name-only)

Tip: If #2, then make a script and put it in .bashrc!

~/scripts/search.sh

run() {
 git grep --cached "$@" $(git diff --cached --name-only)
}

~/.bashrc

# Script to search after string in all staged files.
alias search="~/scripts/search.sh"

$ search test

PatricNox
  • 3,306
  • 1
  • 17
  • 25
1
  1. Right-click on a tab and click "Close All" (not always needed)

  2. Press cmd/ctrl+shift+p to see editor commands

  3. Select "Git: Open All Changes" (which opens all changed files)

Screenshot of editor command "Open All Changes"

  1. Click magnifying glass to open search on left

Search icon on left

  1. Click book icon to select "Search only in Open Editors"

Book icon

Search area with book icon

  1. Search away :)

Note 1: In this screenshot I was searching for "console.log".

Note 2: It doesn't matter if the "files to include" line is blank.

Marcus
  • 3,459
  • 1
  • 26
  • 25