Delete a commit in gitlab

Viewed 49

I pushed a commit to our central repo. Let's call it commit AAAA. Then I realized I wasn't quite satisfied with it. I want to get it out of the repo. I have no command line access to the central repo, just the gitlab GUI. I found a question on this site where someone was talking about using the "revert" option in the gitlab GUI. I tried it. It actually created a new commit that just undid the changes in commit AAAA. Let's call this one BBBB. This is not what I need. I just simply need AAAA gone, and, of course, BBBB along with it. Is there some way to do this in the gitlab GUI? As long as these new commits are there, git thinks my local repo is behind the central repo and I can't do any more pushing.

P.S. Trying to force a reset.

> git reset --hard HEAD~1
> git push -f origin

But I am told, "You are not allowed to force push code to a protected branch on this project." And I look and, yes, the master branch is protected. I have permissions to set the protection status, if I only knew how to do it. Anyone know how? This would solve my problem, it seems. And if anyone has any info on my first question, I would still be grateful to hear it.

1 Answers

Your only option is force push if you want to remove commits from the git history. You cannot delete commits in the GUI or take other actions that would require a force push.

It actually created a new commit that just undid the changes in commit AAAA

That's probably the behavior you want in most cases, especially for protected branches like main/master. If you rewrite the history of that branch, you will create bigger problems for other users who already cloned those would-be-deleted commits. That's partly why repository owners configure protected branch rules to begin with -- to prevent you from doing exactly that.

If you decide you really want to remove those commits from the protected branch, you must remove the protected branch rule that prevents force pushing, remove the commits locally, then force push to the repo. You must have the maintainer role or higher on the project in order to do this.

Related