我非常不擅长 shell 脚本(使用 bash),我正在寻找一种方法来 检查当前 git 分支是否为“x”,如果不是“x”则中止脚本。
#!/usr/bin/env bash
CURRENT_BRANCH="$(git branch)"
if [[ "$CURRENT_BRANCH" -ne "master" ]]; then
echo "Aborting script because you are not on the master branch."
return; # I need to abort here!
fi
echo "foo"
但这不太对
最佳答案
使用git rev-parse --abbrev-ref HEAD
获取当前分支的名称。
那么只需简单地比较脚本中的值即可:
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
if [[ "$BRANCH" != "x" ]]; then
echo 'Aborting script';
exit 1;
fi
echo 'Do stuff';
关于git - 用于检查当前 git 分支是否为 "x"的 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37890510/