Bash

通過 shell 腳本檢查 git 儲存庫的主分支是否落後於原點

  • February 28, 2019

我目前正在編寫一個 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?])

有人有什麼想法嗎?

您應該使用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

您可以查看實際輸出,而不是查看退出狀態

例如

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

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

引用自:https://unix.stackexchange.com/questions/503473