通过 shell 脚本检查 git 存储库的 master 分支是否位于 origin 后面

通过 shell 脚本检查 git 存储库的 master 分支是否位于 origin 后面

我目前正在编写一个 bash 脚本,以提醒我在发现本地 master 分支位于 origin/master 之后时重新设置 git repos 的基础。

到目前为止,我已经提出了以下内容,但$?总是返回1(我怀疑这是因为即使是空的 diff 仍然会加载less.

#!/bin/bash

REPO_PATH=$1
cd $REPO_PATH

# flow once inside repo
{
  git fetch
  git diff master origin/master
} &> /dev/null

if [ "" = $? ]
then
  echo "Empty"
  # logic to follow
else
  {
  git pull
  } &> /dev/null
  echo "Don't forget to rebase!!!"
  echo $REPO_PATH
fi

# check for changes to master
# If master is behind origin/master
# then pull master and notify me to rebase

# run this at the start of the day (this script should be run from my start_work
# script and should also be periodically run throughout the day. [maybe every
# time I'm about to run coverage/push?])

有人有什么想法吗?

答案1

你应该使用git-merge-base--is-ancestor测试:

if git merge-base --is-ancestor origin/master master; then
    echo Empty
else
    echo "Don't forget to rebase!"
fi

答案2

您可以查看实际输出,而不是查看退出状态

例如

git fetch &> /dev/null
diffs=$(git diff master origin/master)

if [ -z "$diffs" ]
then
  echo "Empty"

相关内容