akonsu
akonsu

Reputation: 29538

undo last git commit that is pushed to origin

I have committed wrong files to my branch and pushed it to origin. I have seen the article at How to undo last commit(s) in Git? that deals with undoing a local commit, but my problem is that I have pushed the commit to origin. How to undo this?

Upvotes: 14

Views: 14504

Answers (3)

darmis
darmis

Reputation: 3301

Let's say it is your own feature branch, so what you could do is the following:

  1. Undo your changes locally.
  2. Make the changes you need and make a new commit.
  3. Push with the force flag. This would overwrite history though, but it is your own feature branch!
git reset --soft head~1
git commit -m "Ammending previos commit"
git push -f

I hope this helps someone there out in the wild :)

Upvotes: 0

gtrig
gtrig

Reputation: 12958

Since you've already pushed to origin, your change has been published for others to see and pull from. Because of this, you probably do not want to rewrite the history. So the best command to use is git revert.

This creates a new commit that reverses the changes you made. Push the new commit and origin will be fixed.

Here is an SO answer that gives more details on this.

Upvotes: 10

Matt Bryant
Matt Bryant

Reputation: 4961

git reset HEAD^
git push origin +HEAD

should work for you. See the git-push and git-reset documentation for more information on why.

Upvotes: 17

Related Questions