2016-09-27 80 views
2

我是Git的新手。保存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 

但它總是產生一個空白文件。拉作品就好壽...

有誰知道如何使輸出文件收到它出現在終端上的全部信息:

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' 

回答

2

UNIX shell默認提供兩個輸出流 - stdout和stderr。

這通常很有用,因爲當您將輸出重定向到其他東西時,仍然希望錯誤進入屏幕。

$ cat nosuchfile | grep something 
cat: nosuchfile: No such file or directory 

這就是我想要的。我不希望cat: nosuchfile: No such file or directory被送入grep

如你所知,你可以使用標準輸出和>重定向|

可以使用2>重定向標準錯誤:

$ cat nosuchfile > outfile 2>errormessage 

一個常見的成語是:

$ somecommand > output 2>&1 

這裏&1是指通過標準輸出使用的文件描述符。所以你告訴shell將stderr發送到stdout所在的地方。

您可以使用2>&1將stderr發送到您的輸出文件。或者,您可以使用您在此學到的知識來了解git文檔重新編號--porcelain,或者設計一些其他解決方案,例如將stderr發送到適當的第二個文件。

+0

非常感謝你的完整示例! – Manddy

3

stderr重定向到文件,以及:

git push -v >> logs/logPush.log 2>&1 

看起來git push具有用於此目的--porcelain選項:

- 瓷器

生產機器可讀輸出。每個ref的輸出狀態行將被製表符分隔併發送到stdout而不是stderr。 將給出參考文獻的完整符號名稱。

+0

我用它,它的工作!非常感謝你! – Manddy

3

我猜想git push的退出狀態會顯示它是否成功。您應該使用它,而不是解析日誌:

if ! git push -v >> logs/logPush.log 2>&1 
then 
    echo "${RED}Failed to push. Exiting.${NC}" 
    read 
    exit 
fi 

我用2>&1到stderr重定向到stdout,因此日誌將包含兩個輸出 - 這是可選的。

如果命令失敗,它不一定表示衝突,所以我將錯誤消息修改爲更通用的東西。

+1

這是正確的做法。你不應該依賴用於人類閱讀的信息 - 如果git被翻譯成另一種語言,它會中斷 –