10

From here http://blog.prabir.me/post/Undo-in-Git.aspx, it said

This undo’s your commit and also resets your working tree to the last commit.

1 git reset --hard HEAD^

But how can I un-do my last commit AFTER I did a 'git push'?

Thank you.

When I do 'git log', I get as my top commit

commit 29fc764693a5933a379169e22891e4b0d3d2426f
Merge: 002db49 cfb1d8f

How can I 'git revert' that change?

I get this

$ git revert HEAD 
fatal: Commit 29fc764693a5933a379169e22891e4b0d3d2426f is a merge but no -m option was given.
michael
  • 106,540
  • 116
  • 246
  • 346

4 Answers4

16

you can use git revert HEAD, which generates a new commit, which will undo the changes in your previous commit. Look here. Note however that both the commit and the revert commit will show up in the history.

Edit: As KingChrunch mentioned, when reverting a merge commit, you need to specify which parent you want to revert to, so add -m <parent>. Of course simply following the link I have posted would have told you so.

You can't (well actually shouldn't) modify the history of a shared repository. If you where inclined to modify the history (please don't), you can use git reset with git push --force, or git rebase.

Grizzly
  • 19,595
  • 4
  • 60
  • 78
  • What does it mean by 'fatal: Commit 29fc764693a5933a379169e22891e4b0d3d2426f is a merge but no -m option was given.' I get this error when I do 'git revert HEAD'? – michael Jan 03 '12 at 18:27
  • 1
    `git revert` returns to a previous commit. If you want to revert a merge commit thats not that easy, because it has at least 2 parent commits and nobody can say which one is the branch and which is/are the merge parents. You must choose to which one you want to commit using the `-m `-option. Have a look at `git help revert`. – KingCrunch Jan 03 '12 at 18:35
2

You should git revert the commit.

You can also git push --force the branch after the git reset, but if anyone pulled your branch before that, your histories will have diverged, so you really shouldn't do this.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Ok. But I did a 'git commit' and then 'git push', since I want to roll back my change I did 'git reset --soft HEAD^' and I want to test my code without that change, I did 'git stash' build and test'. Now do I need to do git stash pop, before I do a 'git revert'? – michael Jan 03 '12 at 18:05
1

If you're fine with rewriting history, this one liner skips the need for git reset

git push --force <remotename> HEAD~1:<branchname>

Ref: How can I push a specific commit to a remote, and not previous commits?

Rufus
  • 5,111
  • 4
  • 28
  • 45
0

Just found an even easier method if you're using GitLab and a code review/merge request step. Found this after I accidentally included an unnecessary file in a commit & push. You can also use this method to make edits and recommit them to your branch before merging.

Our process: After coding, we commit, fetch from upstream, rebase, push to GitLab. This creates a branch on GitLab, we then create a merge request for that branch. This is to ensure all code gets code reviewed by a peer before going in the main branch. In this case it was an extremely useful step as my peer review colleague spotted my gaff.

Note: This is only an option BEFORE a merge request is accepted.

To remove a file from a commit after pushing to GitLab and BEFORE merging:

  1. Open the GitLab merge request
  2. Select the 'Changes' tab
  3. Find the file that is unwanted in the commit
  4. Click the 'View file' button for the file
  5. Click the 'Delete' button
  6. Enter the commit information and commit the change

Your merge request has now been fixed and the unwanted file removed from your commit. You can now merge (if the rest pass code review).

You can also edit and recommit files this way. It's an easy way to make quick fixes without needing to open Eclipse/IntelliJ (whatever other tool you use) and checking out the branch.

This just saved me hours of pain avoiding dealing with removing a file from a pushed branch in Eclipse.

Manda QoP
  • 11
  • 4