5

I have pushed a commit by mistake to my Gitlab. How can I undo it?

see image

David Buck
  • 3,752
  • 35
  • 31
  • 35
Chen Hanhan
  • 1,549
  • 6
  • 15
  • 20
  • Search function helps you a lot ;) [Answer by rednaw](https://stackoverflow.com/questions/22682870/git-undo-pushed-commits) – MartinLeitgeb Aug 28 '17 at 14:02

3 Answers3

6

Easy way is to click the revert button in the merged merge request.

Click for Reference docs

Steps to revert a merge request from UI:

  1. Click revert button This creates a new branch rever-some_sha.2
  2. Either opt for a new merge request and submit that.

[ Or ]

  1. Checkout to revert-some_sha locally, add any changes you wanted.
  2. Create a merge request and click merge to master.

Recommendation: Do a periodical rebase of your branch to be on top of master. Which avoids any conflicts and helps to catch any failing tests before even merging your branch.

Community
  • 1
  • 1
bh4r4th
  • 3,760
  • 1
  • 21
  • 25
  • 1
    I get the following error when I try to revert a MR: "Sorry, we cannot revert this merge request automatically. This merge request may already have been reverted, or a more recent commit may have updated some of its content." Does this mean I have to create a new MR manually the changes everything back? – HisDivineShadow Jun 03 '19 at 16:38
  • I have another MR that I'm trying to revert (that's not as old as the one I'm referring to in the above comment) and I'm getting a "Deadline Exceeded" message. What does that mean? – HisDivineShadow Jun 03 '19 at 16:53
  • @HisDivineShadow Bit confusing. MR1 you pushed and when you try to revert, you have got `Sorry, we cannot revret....`. Then you have another MR2 to revert, which threw `Deadline Exceeds`. Correct me if I am wrong. If this the case, ask all the people to stop who are pushing to this master branch. Check the commit tree or use `source tree`. cherry pick the commits to revert or reset. check this https://stackoverflow.com/questions/34572096/how-do-cherry-pick-and-revert-work – bh4r4th Jun 04 '19 at 07:04
  • @HisDivineShadow You have to do it manually locally, before you push anything to remote. As I assume, your remote is already messed up and you shouldn't be over complicating it. – bh4r4th Jun 04 '19 at 07:06
  • I should maybe have been a little more clear. The MR page has a Revert button on it. For both of the cases above I got those errors, I just wanted to know what they meant. I eventually created new local branches and used the `git revert` command to revert the commits then pushed the changes and created new Merge Requests, – HisDivineShadow Jun 05 '19 at 13:52
  • Interesting. Usually it creates a new branch to allow the revert as a revert commit. But, if you delayed or already there are incoming commits then you might see the error 1 that you mentioned and prompts to perform manual revert. I am unaware of the second error. Sorry. – bh4r4th Jun 05 '19 at 15:39
  • Note that revert does not "go backwards"! You are *not* undoing commits with `revert`. Revert applies more commits on top to put back the effect of the previous commit. – NeilG Mar 11 '20 at 05:19
1

I quote rednaw:

You can revert commits with git revert . This will create a new commit which reverts the changes of the commit you specified with the .

Note that you only revert that specific commit, and not commits after that. If you want to revert a range of commits, you can do it like this:

git revert <oldest_commit_hash>..<latest_commit_hash>

Just note that this command is a little bit funny. It actually doesn't revert the commit specified with itself, but the commits after that until and including .

Look at the git-revert man page for more information about the git revert command. Also look at this answer for more information about reverting commits.

Note that this revert command also deletes the corresponding local files

-2

Assuming that you mistakenly created a merge request on Gitlab, All you have to do is scroll to the bottom of the merge request page and click on the button close merge request

Peter Olu
  • 47
  • 1
  • 3