0

I was trying to run this command (on a Mac)...

$ git grep -h | grep dir

Instead of printing the lines containing dir, it prints all the text for git grep -h, and effectively ignores | grep dir.

I also tried this...

$ git grep -h > foo.txt

...which prints it prints all the text for git grep -h, and creates an empty text file.

What's going on here?

popedotninja
  • 1,170
  • 1
  • 12
  • 23
  • 1
    You are running an incomplete command, so `git grep` is writing its help message to standard error, rather than writing actual output to standard output. – chepner May 07 '15 at 21:23
  • `git grep -h` without a pipe does the same as with a pipe. – Matthieu Moy May 08 '15 at 08:13

2 Answers2

1

I believe you need to put your regex after the command like so: git grep -h dir

Then you can pipe it to an output file or do anything else with that data.

jojo
  • 1,135
  • 1
  • 15
  • 35
1

git grep expects a pattern to search. Because you didn't provide it, it displays the help on stderr (while you pipe or redirect the stdout).

The command you need to run to accomplish your goal is:

$ git grep -h dir
axiac
  • 68,258
  • 9
  • 99
  • 134
  • thanks. I couldn't get `git grep -h dir` to work, but you were correct about stderr. I search for stderr answers, found [this question](http://stackoverflow.com/questions/2342826/how-to-pipe-stderr-and-not-stdout), and solved the problem with `git grep -h 2>&1 >/dev/null | grep 'dir'`. – popedotninja May 07 '15 at 22:06