vscode search for a text only in git changes

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?

Upvotes: 48

Views: 13679

Answers (8)

solisoares
solisoares

Reputation: 25

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.

Upvotes: 0

Zorro
Zorro

Reputation: 1519

The simplest way is to exclude .git/

enter image description here

Upvotes: 0

Marcus
Marcus

Reputation: 3797

  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.

Upvotes: 3

Buszmen
Buszmen

Reputation: 2426

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

Upvotes: 57

PatricNox
PatricNox

Reputation: 3948

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

Upvotes: 1

Nicky McCurdy
Nicky McCurdy

Reputation: 19573

  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.

Upvotes: 7

LeGEC
LeGEC

Reputation: 52141

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.

Upvotes: 1

Kunal Vohra
Kunal Vohra

Reputation: 2846

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.

Upvotes: 0

Related Questions