将 Git Push 保存到输出文件

Man*_*ddy 2 linux git bash shell

我是 Git 的新手。

我的问题是,使用 shell 脚本(在 Windows 中运行)我需要将 Git Push 命令保存到输出文件。

到目前为止,我有这样的事情:

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

if grep -q -w -i "Rejected" logs/logPush.log ; 
then 
    echo "${RED}A conflict has been detected. Exiting.${NC}" 
    read
    exit
else
    :
fi
Run Code Online (Sandbox Code Playgroud)

但它总是生成一个空白文件。Pull 工作得很好,但...

有谁知道如何使输出文件接收它出现在终端上的全部信息:

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'
Run Code Online (Sandbox Code Playgroud)

Eug*_*ash 6

也将 stderr 重定向到文件:

git push -v >> logs/logPush.log 2>&1
Run Code Online (Sandbox Code Playgroud)

看起来有用于此目的git push--porcelain选项:

- 瓷

生成机器可读的输出。每个 ref 的输出状态行将以制表符分隔并发送到 stdout 而不是 stderr。将给出参考文献的完整符号名称。


sli*_*lim 5

默认情况下,UNIX shell 提供两个输出流——stdout 和 stderr。

这通常很有用,因为当您将输出重定向到其他内容时,您仍然希望错误显示在屏幕上。

 $ cat nosuchfile | grep something
 cat: nosuchfile: No such file or directory
Run Code Online (Sandbox Code Playgroud)

这就是我想要的。我不想cat: nosuchfile: No such file or directory被喂进去grep

如您所知,您可以使用>and重定向标准输出|

您可以使用2>以下命令重定向 stderr :

$ cat nosuchfile > outfile 2>errormessage
Run Code Online (Sandbox Code Playgroud)

一个常见的成语是:

$ somecommand > output 2>&1
Run Code Online (Sandbox Code Playgroud)

这里&1指的是stdout使用的文件描述符。所以你告诉 shell 将 stderr 发送到与 stdout 相同的地方。

您可以使用2>&1将 stderr 发送到输出文件。或者,您可以使用在此处学到的知识来理解 git 文档 re --porcelain,或设计一些其他解决方案,例如在适当的情况下将 stderr 发送到第二个文件。