I assume that no one intervenes between your work and that you poorly capture the continuous range in repo history. Otherwise, you will have to get complicated. Suppose your story looks like this:
e82401b - (master, HEAD) My most recent private commit ... bc2da37 - My first private commit cf3a183 - (origin/master) My most recent bad public commit ... 292acf1 - My first bad public commit 82edb2a - The last good public commit
The first thing we want to do is knock down commits that you haven't made public yet. You can do this with the following command ( note that your changes will disappear and should be considered unrecoverable ):
git reset
Equivalent (and more readable):
git reset
Now your view of the repository corresponds to the view in origin/master . Now you want to revert your bad public changes and post them as a commit revert. These instructions are for creating one re-commit.
You can use git revert --no-commit a..b to return all commits starting from commit after a (note that!) And ending with, and turn on, commit b . A reversal will be carried out for you. So here we will do:
git revert --no-commit 82edb2a..HEAD
Or, equivalently:
git revert --no-commit 292acf1^..HEAD
Remembering that HEAD now points to the same place as origin/master .
After running the revert command, you now have your settings set and ready to commit, so just run the simple git commit -m "Reverting those bad changes I accidentally pushed and made public" .
Chris hayes
source share