如何让 git 将输出内容输出到文件?

如何让 git 将输出内容输出到文件?

我想git 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

感谢您的帮助!


我刚刚找到了解决方案:

第1部分

(感谢甜点答案)
git从设计上来说永远不会写入stdoutstderr。所以我也需要重定向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 将我最感兴趣的进度部分的行写入 finally 中,stderr所以我现在可以使用

git clone --progress XYZ &> git_clone.file

答案2

git clone用于stderr输出,所以只需写到文件:

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

或者,你可以重定向两者stdout——stderr这在本例中不是必需的,但这样你就可以确保每一个该命令产生的输出被重定向:

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

如果git clone重定向后输出明显不同,则终端中运行的整个进度信息不会包含在输出文件中。这是设计使然,如果我没记错的话,你无法直接轻松更改该行为,然而如果你需要另一个脚本的输出,你很可能管道它运行正常并且给出了所有的输出:

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

在你的脚本中你可以stdin使用-,例如cat -打印stdinstdout——更多信息请参见这里:如何编写一个接受来自文件或标准输入的脚本?如何在 Bash 中从文件或标准输入读取?

相关内容