0

I was following this SO question here but something is not working for me...

How to undo last commit(s) in Git?

I do a git reset --hard HEAD~1 three times and I see the code I want and when I do a git status, it says You branch is behind origin/master by 3 commits and can be fast-forwarded. Yes, I backed out of 3 commits so that seems good but how do I now commit and push the changes so the latest in master in my remote repo is without the 3 commits.

thanks, Dean

Community
  • 1
  • 1
Dean Hiller
  • 19,235
  • 25
  • 129
  • 212

2 Answers2

4

You can run

git push --force

To force push the remote repo backwards in time.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

If nobody has yet pulled the state of your origin repo, go with the answer provided by SLaks.

But if you're not sure about that, forced pushing is not the best idea since it could cause problems with your fellow developers.

To have your last three commits reverted in a way that you could use with origin, go with git revert. This will create a new commit reverting your already made commits.

If your history looks like this

A--B--C--D--E--HEAD

and you want to drop C, D and E, revert them in the following sequence

git revert E
git revert D
git revert C

Then, you'll be 3 commits ahead origin and you could push without any hassle. If someone already pulled your changes of C, D, and E, they will be removed upon the next pull.


By the way: this is what the not-accepted answer to your referenced question also recommends:
https://stackoverflow.com/a/6376039/520162 (Undo a public commit)

Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201
  • Special mention for warning about changing a possibly shared history, and for advising a `git revert`. – Romain Dec 17 '12 at 15:48
  • that didn't work, I ended up with conflicts from a merge (ON A REVERT). I am not sure why git thinks it needs to merge as I just want that exact code base as my latest commit. ie. this completely did not work :(. thanks though. – Dean Hiller Dec 17 '12 at 16:08