如何将远程分支"rollback"到特定标记

  • 本文关键字:rollback 分支 程分支 git
  • 更新时间 :
  • 英文 :


我有一个分支,每个部署都被标记。万一发生意外,我想"回滚"。我的分支到前一个工作标签。

我尝试了以下操作,但没有成功。这感觉就像我在一个非常基本的层面上做错了什么,但我真的不明白是什么。

# Checking if the tag is on the branch with
git tag --merged $BRANCH --sort=taggerdate --list
# Trying to rollback with
git push --force origin refs/tags/$PREVIOUS_TAG:refs/heads/$BRANCH

结果

To https://github.com/me/myrepo
! [remote rejected]   my_previous_tag -> my_branch (bad ref)
error: failed to push some refs to 'https://github.com/me/myrepo'

编辑:一个简短的例子,试图解释我正在做什么

我有一个branch branch,上面有以下标签:

  • deployment_1
  • deployment_2
  • deployment_3

每个指向不同的提交。我想把我的远程分支带回到

  • deployment_1
  • deployment_2

并删除之后的所有提交。就像我要硬复位一样。

很可能你的标签是一个带注释的标签(检查git cat-file -t "$PREVIOUS_TAG"-它输出tag为带注释的标签和commit为轻量级标签)。您需要从它中解包裹提交-并且您应该真正包含完整的错误消息,其中包括以下重要部分:

remote: error: cannot update ref 'refs/heads/my-branch': trying to write non-commit object…分支'refs/heads/my-branch'

只有提交可以推送到分支参考:

git push origin "+$PREVIOUS_TAG^{commit}:refs/heads/$BRANCH"