Reputation: 110670
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
Reputation: 175
I prefer to do the revert from Visual Studio directly.
Upvotes: 0
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
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:
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
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
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