如何将`git clone`的完整输出重定向到文件?

时间:2016-06-07 01:23:36

标签: git shell unix pipe

当我像往常一样运行git clone时,我看到了:

$ git clone https://github.com/bensmithett/webpack-css-example
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

但是,当我尝试将其重定向到文件时,我只看到这个:

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

以下是我的尝试:

$ git clone https://github.com/bensmithett/webpack-css-example 2>&1 | tee out.log
$ cat out.log
Cloning into 'sample-data'...

我也在Node.js中尝试过它,它做了同样的事情:

const fs = require('fs');
const child = spawn('git clone https://github.com/bensmithett/webpack-css-example');
child.stdout.on('data', function(data){
  console.log(String(data));
});
child.stderr.on('data', function(data){
  console.log(String(data));
});
// Cloning into 'webpack-css-example'...

为什么所有remote:等东西都没有被管道传输到stdin / stderr?有没有办法捕获输出?如果没有,发生了什么使得输出显示在终端中,但它没有通过stdout或stderr传递?

1 个答案:

答案 0 :(得分:9)

默认情况下,只有当标准错误流定向到终端时,Git才会显示克隆进度。由于您将其重定向到管道,因此输出流不再连接到终端。因此,为了捕获输出,您需要添加--progress参数来强制进度状态,例如

git clone --progress https://github.com/foo/bar 2> out.log

请参阅:man git-clone

  

--progress

     

除非指定-q,否则默认情况下会在标准错误流上报告进度状态。即使标准错误流未定向到终端,此标志也会强制进度状态。

要以任何其他方式强制终端,您必须预加载某个库以强制isatty()始终返回true(请参阅:man isatty)。 git在source code

中使用此函数