gpt4 book ai didi

git - 用于检查当前 git 分支是否为 "x"的 bash 脚本

转载 作者:IT王子 更新时间:2023-10-29 01:29:40 27 4
gpt4 key购买 nike

我非常不擅长 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com