Exclude .git directory from grep searches
From raju
Objective
The idea here is to recursively search for a word using grep but exclude everything under .git directory
bare bones solution
grep -sir pattern . --exclude-dir=".git"
The . in the ".git" argument is matched literally. So it will exclude hits from .git but include hits from, say, kgit directory. The appendix contains more details on this.
Advanced solution
Typing the whole grep command can become cumbersome after a while. So I wrote git_grep.py, a python script to simplify searching in a repository while working in it.
If a directory name is supplied, the script figures out the top level git directory to search in. If it is not supplied, it starts with the current working directory.
For example, I have a git repository in ~/work/gitlab/rutils.
rajulocal@hogwarts ~/work/gitlab/rutils % ls -ald .git drwxr-xr-x 8 rajulocal rajulocal 4096 Mar 5 08:51 .git/
To search this directory while working in it
rajulocal@hogwarts ~/work/gitlab/rutils % git_grep.py "import os" /home/rajulocal/work/gitlab/rutils/python3/scottrade/scottrade.py: import os /home/rajulocal/work/gitlab/rutils/python3/git_grep.py:import os
Sometimes we might be working in a directory that is part of the repository but buried deep underneath. If so, the script figures out the top level directory and searches from there.
rajulocal@hogwarts ~/work/gitlab/rutils % cd python3/scottrade rajulocal@hogwarts ~/work/gitlab/rutils/python3/scottrade % git_grep.py "import os" /home/rajulocal/work/gitlab/rutils/python3/scottrade/scottrade.py: import os /home/rajulocal/work/gitlab/rutils/python3/git_grep.py:import os
If you are not inside a git repository, but want to search in one simply pass it as an argument.
rajulocal@hogwarts ~/work/gitlab/rutils/python3/scottrade % cd rajulocal@hogwarts ~ % git_grep.py "import os" ~/work/gitlab/rutils/python3 /home/rajulocal/work/gitlab/rutils/python3/scottrade/scottrade.py: import os /home/rajulocal/work/gitlab/rutils/python3/git_grep.py:import os
Appendix - experiment with grep
% mkdir -p .git kgit % echo "more this" > .git/head % echo "more that" > kgit/head % echo "more this and that" > head % grep more . grep: .: Is a directory % grep more * head:more this and that grep: kgit: Is a directory % grep -sir more * head:more this and that kgit/head:more that % grep -sir more . ./kgit/head:more that ./head:more this and that ./.git/head:more this % grep -sir more . --exclude-dir=".git" ./kgit/head:more that ./head:more this and that % grep -sir more . --exclude-dir="kgit" ./head:more this and that ./.git/head:more this % grep -sir more . --exclude-dir="*git" ./head:more this and that
Appendix - system information
% which grep git /bin/grep /usr/bin/git % dpkg -S /bin/grep /usr/bin/git grep: /bin/grep git: /usr/bin/git % dpkg -l grep git | grep ^ii ii git 1:2.1.4-2.1+deb8u1 amd64 fast, scalable, distributed revision control system ii grep 2.20-4.1 amd64 GNU grep, egrep and fgrep % grep --version grep (GNU grep) 2.20 Copyright (C) 2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>. rajulocal@hogwarts ~/work/gitlab/rutils % git --version git version 2.1.4