git - 如何响应 bashrc 函数的 git 输出?

标签 git bash shell github

我必须使用多个 git 存储库,并且我希望能够将它们全部切换到某个分支。到目前为止,由于我对 bash 脚本的了解有限,我做了这个:

# Git switch all repos to a given branch.
switchall() {
    # Reset OPTIND so we can call this function multiple times.
    local OPTIND

    while getopts "b:" OPTION
        do
            case $OPTION in
                b)
                    echo "Switching all GIT repo's to: $OPTARG"
                    for d in ~/git/*/ 
                    do
                        ( cd $d && git checkout $OPTARG )
                    done
                    ;;
            esac
        done
}

但是假设我想将所有存储库更改为某个分支,但其中一些存储库没有我想要更改为的特定分支。我会收到这样的错误:

error: pathspec 'test' did not match any file(s) known to git.

当我运行时会发生这种情况:

switchall -b test

如何捕获错误消息并抑制它或根据已输出的特定错误消息执行某些操作?

最佳答案

类似的事情:

( cd $d && git checkout $OPTARG 2>/dev/null )
if ! [ $? -eq 0 ]
then
    echo things went wrong
fi

甚至:

if ! ( cd $d && git checkout $OPTARG 2>/dev/null )
then
    echo things went wrong
fi

正如在特殊参数部分下的man bash中一样:

? Expands to the exit status of the most recently executed foreground pipeline.

关于git - 如何响应 bashrc 函数的 git 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40461745/

相关文章:

bash - 如何使用带分隔符 "{##} "的剪切?

linux - 使用 Wget 获取长 URL

eclipse - *.bat 从 Eclipse 打开的文件在错误的目录中打开

git - 使用 .gitignore 不提交 bin、debug 和 config 文件夹

bash - 除了移动到每个目录之外,是否有 git 或 bash 方式来提取多个 git 存储库?

git - 如何从 git 中删除 sublime 编辑器

c++ - 获取C++中终端运行命令的返回值

git - 在 Perforce 中,如何像 Git 一样在分支之间切换?

python - 如何告诉 bash 专门转义 Python 脚本中的通配符

linux - 如何从文本文件添加用户并生成随 secret 码 Fedora