michael
michael

Reputation: 110670

How can I un-do a git commit AFTER a git push?

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.

Upvotes: 11

Views: 36981

Answers (5)

Tim Swift
Tim Swift

Reputation: 175

I prefer to do the revert from Visual Studio directly.

  1. Go to the "Git Changes" tab.
  2. Select the "View all commits" link at the top.
  3. Expand the "Outgoing" Branch to see your Push.
  4. Right click the commit and select "Revert".

Upvotes: 0

Rufus
Rufus

Reputation: 5566

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?

Upvotes: 1

Manda QoP
Manda QoP

Reputation: 11

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.

Upvotes: 0

Grizzly
Grizzly

Reputation: 20211

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.

Upvotes: 18

Fred Foo
Fred Foo

Reputation: 363817

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.

Upvotes: 2

Related Questions