Git 尝试 checkout 分支或默认分支

标签 git devops

假设我在远程存储库上有这个分支:

./master
./develop
./feature/A
./feature/B

我想 pull 特定分支,如果该分支不存在,我想默认为开发或掌握。 如果功能分支不存在,则开发是默认后备 如果开发不存在,master 是默认后备。

我工作的项目有很多存储库,但只有其中一些有功能分支。 其他存储库有主分支和集成分支,有些只有主分支。

我想编写一个脚本,该脚本将 pull 具有特定分支(feature/B)的所有存储库,如果当前存储库没有该分支,我想 pull 开发或掌握。

最佳答案

简单的 bash OR 对你有用吗?

 git checkout foo || git checkout master
error: pathspec 'foo' did not match any file(s) known to git
Switched to branch 'master'
Your branch is behind 'origin/master' by 30 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

基本上,如果git checkout foo成功 - 完成。如果失败,调用git checkout master

关于Git 尝试 checkout 分支或默认分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70436600/

相关文章:

git - 如何抑制 git clone 输出?

azure - 有没有办法拒绝取消构建管道 Azure DevOps 的能力?

ruby-on-rails - 尝试使用新主题构建或提供服务时出现 Git 相关错误

azure - 是否可以根据 Azure DevOps 中的其他字段选择自动填充字段?

c# - Azure DevOps 构建管道花费的时间太长(25-30 分钟)

docker - 如何从jenkinsfile构建多个docker容器?

security - 用 gitlab 卑鄙地打嗝

ruby - 错误 : "fatal: I don' t handle protocol `` git` when using bundle install

git - 在分支上工作时(新壮举),我是 pull 入更新还是 merge 它们?

git - GIT 是否基于日期时间 merge ?