首页 > 解决方案 > 如何将git合并失败输出到文件

问题描述

我已经设置了一个脚本来定期 rebase 和合并子分支的开发分支列表中的更改:

while read -r child
do
  echo "Merging development to $child" 2>&1 >> $HOME/logs/git_rebase.log
  git checkout $child
  git status
  echo "Commencing merge" 2>&1 >> $HOME/logs/git_rebase.log

  if [[ ! $(git merge --progress -m 'merge development to child branch' --strategy-option theirs origin/development_does_not_exst 1>/dev/null 2>error.log) ]]; then
    echo "git merge for $child failed with the following log:"
    echo `git log -1`
  else
    echo "merge successful"
  fi

  #git push
done < "../branchlist.txt"

该项目是多模块的,因此开发中的任何更改都只会影响其中的一个模块。由于我的合并策略,我不太担心会出现冲突;对 branchlist.txt 中分支的唯一更改应该是从开发分支引入到父模块的更改。但是,我想知道这是否因任何原因而失败。因此,我想将任何错误输出到 error.log,然后将其内容通过电子邮件发送给我自己。

到目前为止,虽然我的 if 语句似乎可以检测故障(使用不存在的分支进行测试),但我无法将控制台输出重定向到日志文件。有没有人做过类似的事情?

标签: gitloggingerror-handlingmerge

解决方案


我想到了; >> error.log在 echo 语句中使用似乎是一个非常简单的例子:

while read -r child
do
  echo "Merging development to $child" 2>&1 >> $HOME/logs/git_rebase.log
  git checkout $child
  git status
  echo "Commencing merge" 2>&1 >> $HOME/logs/git_rebase.log

  if [[ ! $(git merge --progress -m 'merge development to child branch' --strategy-option theirs origin/development_does_not_exst 1>/dev/null 2>error.log) ]]; then
    echo "git merge for $child failed with the following log:" >> $HOME/logs/error.log
    echo `git log -1` >> $HOME/logs/error.log
  else
    echo "merge successful" >> $HOME/logs/git_rebase.log
  fi

  #git push
done < "../branchlist.txt"

推荐阅读