How to undo merge request with its commits?

gizyk picture gizyk · Dec 12, 2018 · Viewed 7.5k times · Source

I did a merge request to my branch develop on gitlab. Then I realise I had done a huge mistake. I quickly pushed the revert button, and I thougth It's everything ok, It wasn't. Gitlab made only revert commit. In the logs there are still commits from source branch. It causes many issues with version. Is it any way, any solution to revert single merge request on gitlab platform and clean branch logs from many commits which are unwanted. I'd like my branch to be like this merge request never had a place.

Answer

mbuechmann picture mbuechmann · Dec 12, 2018

First create a new branch keepsafe to have all those changes still somewhere in case you mess up. This will "copy" the current state locally and save it on the remote also.

git checkout -b keepsafe
git push

Now go back to develop. X is the number of commits you want to have deleted.

git checkout develop
git reset --hard HEAD~X
git push -f

The will hard reset local develop to the original commit. git push -f will overwrite the remote branch. All commits will be gone.

Note that GitLab allows the administrator to disable force pushes (git push -f), so if that doesn't work you need to talk to your administrator. But then you should probably be doing that anyway.