vscode search for a text only in git changes
Asked Answered
V

8

45

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?

Voluntary answered 15/11, 2019 at 11:9 Comment(2)
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.Continually
Please let us know if command line calls could be acceptable for you: I feel like this can be accomplished with those.Rove
D
50

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 ;)

Dharna answered 12/4, 2021 at 11:54 Comment(6)
That will not open all changed files, it seems to have a hard limit on number of open filesGains
@Gains How many did you try to open?Dharna
113 and it opened like 20Gains
@Gains I've just tried opening 247 and it opened 247 for me. Maybe you've changed some settings or have not enough ram...whatever it is I'm just trying to say it's not a "hard" limit since I can open a lot.Dharna
Unfortunately the workaround won't include results where the search term was deleted in the changes.Travesty
This a) Doesn't search deletions and b) doesn't search only on the lines changed. This is not very useful.Floruit
D
7
  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.

Dyanne answered 17/6, 2020 at 8:16 Comment(0)
U
3
  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.

Ubangi answered 28/4, 2023 at 21:32 Comment(0)
S
1

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.

Strapped answered 17/6, 2020 at 11:50 Comment(4)
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".Gibbosity
I just noticed that this is not yet implemented, so I assume you got that screenshot from here? github.com/microsoft/vscode/issues/20530Gibbosity
Please. That's a user-made implementation to illustrate the issue.Percolation
@Kunal Vohra. Can you update/clarify your post? The feature you mention (in the images) is not implemented in VSCode (yet).Matthewmatthews
T
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.

Thule answered 20/6, 2020 at 22:8 Comment(0)
A
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

Air answered 23/6, 2020 at 11:16 Comment(0)
A
0

The simplest way is to exclude .git/

enter image description here

Amando answered 23/2, 2024 at 11:36 Comment(0)
D
0

Add this alias to you .rc file:

mo () {
    xclip -selection c < <(git diff --name-only | xargs -I % echo -n "%,  ")
    echo "copied modified files to clipboard"
}

This copies all the modified paths to your clipboard, then you just need to paste in the "files to include" area in VSCode.

No need to close your opened files, open SCM, open changes and click the icon.

Delorenzo answered 20/6, 2024 at 17:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.