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

标签 git bash shell sh

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

相关文章:

php - 如何使用 PHP 执行 bash 脚本?

bash - 如何在 CURL shell 脚本中捕获超时/错误?

linux - 环境变量是通过内存中还是磁盘上的 "set-environment"在 Linux 上的 systemd 中创建的?

function - 在 C-Shell 中的 .cshrc 中添加函数

shell - 如何更改 GCE VM 实例中的默认 shell?

git - "exit status 128"试图从 golang 创建 git 分支

windows - Windows下的git svn clone large repo : out of memory - not a large file issue

git - 当外部修改文件时,为什么在 emacs 中重新加载通知很慢?

git - 是否有任何可下载的体面的 git 存储库管理器?

shell - 比较 Busybox ash 中的子字符串