Git: unable to redirect/parse the output of 'g

2019-05-06 13:17发布

问题:

What is special about the output message of git fetch command that is printed on the console? I'm not able to use grep, xargs, etc. Not able to redirect the output to a file also..

Note: I'm using git fetch --dry-run command

[sangeeth@localhost santest-code]$ 
[sangeeth@localhost santest-code]$ git fetch --dry-run > /tmp/1
From ssh://git.code.sf.net/p/santest/code
   9f068d0..2b9dc4e  master     -> origin/master
[sangeeth@localhost santest-code]$ 
[sangeeth@localhost santest-code]$ cat /tmp/1              <= no data
[sangeeth@localhost santest-code]$  
[sangeeth@localhost santest-code]$ git fetch --dry-run | grep "ssh"     <= output has both lines
From ssh://git.code.sf.net/p/santest/code
   9f068d0..2b9dc4e  master     -> origin/master
[sangeeth@localhost santest-code]$ 
[sangeeth@localhost santest-code]$ git --version 
git version 1.7.11.4
[sangeeth@localhost santest-code]$ 

I'm trying to parse the output of git fetch --dry-run command to check whether the local (master) branch is up-to-date with the remote (origin/master) branch.

回答1:

Some of git's status output goes to STDERR. If you want to grep through it, merge STDERR into STDOUT like so:

git fetch --dry-run 2>&1 | grep ssh