如何让git生成输出到文件?

我想用git clonegit clone的输出写入文件

 git clone https://github.com/someRepository > git_clone.file 

但相反,我得到了终端中显示/更新的输出

 Cloning to 'someRepository' ... remote: Counting objects: 2618, done. remote: Compressing objects: 100% (14/14), done. remote: Total 2618 (delta 2), reused 12 (delta 1), pack-reused 2603 Received objects: 100% (2618/2618), 258.95 MiB | 4.39 MiB/s, Done. Resolving Differences auf: 100% (1058/1058), Done. Check Connectivity ... Done. 

但是生成了文件git_clone.file但仍为空。

我最初的目标是将git的输出绕过一个函数(请参阅我的问题 )。 但是现在我意识到git甚至似乎都没有真正产生输出到stdout但是因为没有写入文件而有所不同。

如何从git获取此显示的输出以将其重定向到文件/函数?


编辑

建议的stderr (和stdout )重定向并没有解决问题。

 git clone https://github.com/someRepository 2> git_clone.file git clone https://github.com/someRepository &> git_clone.file git clone https://github.com/someRepository > git_clone.file > 2>&1 

所有人都给了我相同的结果:只有这一行

 Cloning to 'someRepository' ... 

出现在git_clone.file


背景信息

我为什么需要这个?

正如我在其他问题中所解释的那样, 我在输出脚本的底部写了一个自定义进度条。 (我在多个脚本中使用它但是)在这种情况下, 脚本将许多(直到现在的107)git存储库从github迁移到我们自己的Gitlab-Server并修复Git LFS支持,通​​常在没有它的情况下丢失。

所以我想仍然看到git的所有输出,但也希望我的进度条在终端输出的底部工作。

谢谢你的帮助!


我刚刚找到了解决方案:

第1部分

(感谢甜点的回答 )
git by design确实永远不会写入stdout而是stderr 。 所以我也需要重定向stderr ,以便使用输出

 git clone XYZ &> git_clone.file 

第2部分

无论如何这还不够,我只收到文件输出的“无趣”部分,但不是我真正想要的进展线。

man git-clone再次进行进一步的研究我意识到存在一个选项

 --progress 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. 

虽然我认为它实际上已经附加到终端,但现在这似乎迫使git将我最感兴趣的进度部分的行写入stderr ,所以我现在可以使用它们

 git clone --progress XYZ &> git_clone.file 

git clone使用stderr作为输出,所以只需将写入文件:

 git clone https://github.com/someRepository 2>git_clone.file 

或者,您可以重定向stdoutstderr – 这在此特定情况下不是必需的,但是这样您可以确保命令生成的每个输出都被重定向:

 git clone https://github.com/someRepository &>git_clone.file 

git clone的情况下,如果重定向它会有不同的输出,终端中运行的整个进度信息不包含在输出文件中。 这是设计和IIRC你不能轻易直接改变这种行为, 但是如果你需要在另一个脚本中输出你可以很好地管它到它,它工作得很好,并为您提供所有输出:

 git clone https://github.com/someRepository | cat 

在你的脚本里面你可以得到stdin -例如cat -stdin打印到stdout – 请看这里更多: 如何编写一个接受来自文件或stdin的输入的脚本? 以及如何从Bash中的文件或标准输入读取? 。