Lance Pollard Lance Pollard - 1 year ago 92
Bash Question

How to redirect full output of `git clone` to a file?

When I run git clone like usual, I see this:

$ git clone
Cloning into 'webpack-css-example'...
remote: Counting objects: 179, done.
remote: Total 179 (delta 0), reused 0 (delta 0), pack-reused 179
Receiving objects: 100% (179/179), 24.07 KiB | 0 bytes/s, done.
Resolving deltas: 100% (79/79), done.
Checking connectivity... done

However, when I try to redirect that to a file, I only see this:

Cloning into 'webpack-css-example'...

Here is what I tried:

$ git clone 2>&1 | tee out.log
$ cat out.log
Cloning into 'sample-data'...

I tried it in Node.js as well, and it does the same thing:

const fs = require('fs');
const child = spawn('git clone');
child.stdout.on('data', function(data){
child.stderr.on('data', function(data){
// Cloning into 'webpack-css-example'...

Why is all the
etc. stuff not getting piped to either stdin/stderr? Is there any way to capture the output? If not, what is happening that makes it so the output is displayed in the terminal, yet it is not being passed through either stdout or stderr?

Answer Source

By default Git will display the cloning progress only when the standard error stream is directed to a terminal. Since you're redirecting it to the pipe, the output stream is no longer attached to the terminal. So in order to capture the output, you need to add --progress parameter to force the progress status, e.g.

git clone --progress 2> out.log

See: man git-clone


Progress status is reported on the standard error stream by default when it is attached to a terminal, unless -q is specified. This flag forces progress status even if the standard error stream is not directed to a terminal.

To force the terminal in any other way, you'll have to preload some library to force isatty() to return always true (see: man isatty). This function is used by git across the source code.

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