Manddy Manddy - 1 year ago 106
Bash Question

Saving Git Push to an output file

Good Afternoon!

I'm new to GIT, so please bear with me.
My problem is, with a shell script (running in Windows) i need to save the Git Push commands to an output file.

So far i've something like this:

echo -e "\n6) ${GREEN}Starting Push.${NC}"
git push -v >> logs/logPush.log

if grep -q -w -i "Rejected" logs/logPush.log ;
echo "${RED}A conflict has been detected. Exiting.${NC}"

But it always generates a blank file. The Pull works just fine tho...

Does anyone know how to make the output file receive the whole information that it appears on the terminal:

Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To ssh:repository
42be914..ead1f82 master -> master
updating local tracking ref 'refs/remotes/origin/master'

Answer Source

A UNIX shell provides two output streams by default -- stdout and stderr.

This is often useful because when you redirect output to something else, you still want errors to go to the screen.

 $ cat nosuchfile | grep something
 cat: nosuchfile: No such file or directory

This is what I wanted. I didn't want cat: nosuchfile: No such file or directory to be fed into grep.

As you know, you can redirect stdout using > and |.

You can redirect stderr using 2>:

$ cat nosuchfile > outfile 2>errormessage

A common idiom is:

$ somecommand > output 2>&1

Here &1 refers to the file descriptor used by stdout. So you're telling the shell to send stderr to the same place as stdout.

You can use 2>&1 to send stderr to your output file. Or you can use what you've learned here to make sense of the git documentation re --porcelain, or design some other solution, for example sending stderr to a second file where appropriate.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download