gizyk
gizyk

Reputation: 151

How to undo merge request with its commits?

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.

Upvotes: 9

Views: 15128

Answers (2)

mbuechmann
mbuechmann

Reputation: 5770

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.

Upvotes: 8

Venkataraman R
Venkataraman R

Reputation: 13009

You can revert to previous commit using interactive rebase. Assuming you want to revert HEAD to HEAD~5(5 commits before), you can choose parent of HEAD~5 i.e., HEAD~6 and just apply HEAD~5 on top of it.

git rebase -i HEAD~6

Every commit between HEAD~6 to HEAD will be rewritten. Say HEAD~5 SHA1 is ABC & HEAD~6 SHA1 is XYZ. Current HEAD is PQR. We need to remove other commits from the below edit, as we dont want to rewrite them on HEAD~6. We just want to apply HEAD~5 on top of HEAD~6. We should have at least one entry in this file, otherwise rebase will abort. So, we are applying HEAD~5 on top of HEAD~6.

edit ABC     reverting to state before merge 

# Rebase XYZ..PQR onto XYZ
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Now, issue the below command to amend history and enter commit message.

git commit --amend

Now, issue below command to continue and exit editor

git commit --continue

Read more on rewriting git history

Upvotes: 0

Related Questions