NickyLarson
NickyLarson

Reputation: 137

How to delete commits from gitlab? (Git-revert not working)

I am trying to delete some gitlab commits that were made in error.

the answers provided in other threads were

  1. click on commit name
  2. click "options"
  3. click "revert"

When I attempted to delete the commits, they were not deleted. Instead, two extra merges were created on top. I'm sure this is normal, but I don't understand why it has done that, and more importantly, it hasn't deleted the commits.

How can I delete the commits completely so that the last available commit is the one with the superman logo? thanks

Upvotes: 4

Views: 19259

Answers (1)

zessx
zessx

Reputation: 68810

The non destructive way is simply to do what you've done: revert your commits. Sure there "2 more commits", but your branch is in the same state than before.

The destructive way is to delete commits but this would override your branch history. Because of this you'll need extra rights to rewrite the branch history.

Before to go further, you need to understand that:

  • Depending on the repository configuration you may NOT be allowed to do such a thing
  • You'll force everybody else working on this repository to force pull the branch, therefore they may loose their work if they're not well understanding the process
  • You'll loose EVERY commit after the superman one, even those that would have been created by someone else in the last hours

I highly recommend you to NOT do this.

By code, assuming this is the branch master:

# Retrieve the latest version
git pull origin master
# Goes back to the superman commit
git reset --hard 329a7a0e
# Force push to rewrite history
# Will be refused if the branch is protected
git push origin master --force

With Gitlab, I think you have no other way than:

  • Create a new branch from the 329a7a0e commit, named master-rollback
  • Delete the master branch
  • Create a new branch master from master-rollback
  • Delete the master-rollback branch

Upvotes: 6

Related Questions