Silence git(没有错误,没有输出,一句话也不说)

Silence git (no errors, no output, don't say a word)

我正在使用 git 来管理一些自定义包。如果包不在第一个仓库中,我想 运行 尝试另一个来源的第二个命令。我不需要告诉用户这正在发生。用户不需要知道 git 存在。

if ! git clone git@gitlab.com:username/repo directory -q
        then
            #do something else
        fi

git 即使 -q.

仍然在控制台中打印致命错误

那么,如何让小 git 安静下来?

两种方式:

git clone git@gitlab.com:username/repo directory > /dev/null 2>&1 - 年长 bash

和:

git clone git@gitlab.com:username/repo directory &> /dev/null - 较新的 bash(根据下面 link 的版本 4 以上)

有关详细信息,请阅读 I/O Redirection in Bash。 本质上,您在这里所做的是将 stdoutstderr 重定向到 /dev/null,即 "nowhere".

git 克隆 git@gitlab.com:username/repo 目录 > /dev/null 2>&1 - 旧的 bash

以上会更兼容,