如何检查git在bash脚本中的两个分支之间是否存在差异

velaa5lx  于 2022-12-17  发布在  Git
关注(0)|答案(2)|浏览(212)

如果一个git仓库中的两个分支之间存在差异,我需要检入一个bash脚本。我知道用git diff可以看到差异。但是我需要在if中使用它。
我怎么能这么做呢?
例如:

git diff ......
if [ $? -eq 1 ] 
then
    echo "will do something in here"
fi
zaqlnxep

zaqlnxep1#

(edit:交换了--quiet的退出代码,这会抑制输出)
git diff --quiet将导致命令设置退出代码,类似于正常的diff命令(如果存在差异)。

if ! git diff --quiet <commit1> <commit2> ; then
  echo "Different"
else
  echo "Not different"
fi
k97glaaz

k97glaaz2#

下面是一个例子:

#!/bin/bash

# Set the branch names
BRANCH1="master"
BRANCH2="my-feature-branch"

# Check if there are differences between the two branches
DIFFERENCES=$(git diff --name-only $BRANCH1 $BRANCH2)

# If there are differences, print a message
if [ -n "$DIFFERENCES" ]; then
  echo "There are differences between the $BRANCH1 and $BRANCH2 branches."
else
  echo "There are no differences between the $BRANCH1 and $BRANCH2 branches."
fi

字符串

相关问题