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?
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?
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:
Changes
and choose Open changed files
. Note that it will open also deleted files.Search
and select Search only in Open Editors
Not ideal, but that's how I search for my console.log leftovers ;)
^
, using the View > Terminal
menu option, or
running the View: Toggle Integrated Terminal
command.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.
Right-click on a tab and click "Close All" (not always needed)
Press cmd/ctrl+shift+p to see editor commands
Select "Git: Open All Changes" (which opens all changed files)
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.
Refer these screenshots. You can search in Open files easily.
Also you can use File to include feature
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
.
From the command line :
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
git diff --name-only
will list the files modified between the two target commitsBy 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.
You can't.
Two options:
Suggest this as an feature on VSCode repository.
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
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.
© 2022 - 2025 — McMap. All rights reserved.