Home > Enterprise >  How to "rollback" a remote branch to a specific tag
How to "rollback" a remote branch to a specific tag

Time:01-13

I have a branch were each deployment is tagged. In case of incident I would like to "rollback" my branch to the previous working tag.

I tried the following without any success. It feels like I doing something wrong at a very basic level but I don't really understand what.

# 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

The result

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'

EDIT: A quick example trying to explain what I am trying to do

I have a branch BRANCH with the following tags on it:

  • deployment_1
  • deployment_2
  • deployment_3

Each pointing to different commits. I would like to basically bring back my remote branch to

  • deployment_1
  • deployment_2

and remove all the commits after that. Like I would do with a hard reset.

CodePudding user response:

Likely your tag is an annotated tag (check with git cat-file -t "$PREVIOUS_TAG" – it outputs tag for annotated tags and commit for lightweight tags). You need to unwrap the commit from it – and you should really include the full error message including this important part:

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

Only commits can be pushed to branch refs:

git push origin " $PREVIOUS_TAG^{commit}:refs/heads/$BRANCH"
  •  Tags:  
  • git
  • Related