Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to finish reverting a commit, and how to revert a lot of commits

Tags:

This has to be simple, but I can't find it in git-scm.

I've been making a lot of small commits to a public project, and all of my work is bad. I want to remove everything that I've done. Some I've just committed locally, some I've pushed to 'origin master'.

The first commit (a week ago) is bdbad86... with the most recent being e82401b...

I want to just make all these go away. I've tried to revert one.

git status  
# On branch master  
# You are currently reverting commit e82401b.  
#   (all conflicts fixed: run "git revert --continue")  
#   (use "git revert --abort" to cancel the revert operation)  
  1. I can't figure out how to finish this reverting.
  2. I don't want to have to do each commit separately, I want to blow them all away.
like image 706
Pat Farrell Avatar asked Dec 16 '13 05:12

Pat Farrell


2 Answers

I'm assuming here that no one else interjected commits in between your work, and that you bad commits form a continuous range in the repo's history. Otherwise you're going to have to get more complicated. Let's assume your history 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 blow away the commits that you haven't made public yet. You can do this with the following command (note that your changes will be gone and should be considered unrecoverable):

git reset --hard cf3a183 

Equivalently (and more readable):

git reset--hard origin/master

Now your view of the repository agrees with the view in origin/master. You now want to revert your bad public changes and publish them as a revert commit. These instructions are for creating a single revert commit.

You can use git revert --no-commit a..b to revert all the commits starting at the commit after a (note that!) and ending at, and including, commit b. The reversion will be staged for you to commit. So, here, we would 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 changes staged and ready to commit, so just run a simple git commit -m "Reverting those bad changes I accidentally pushed and made public".

like image 100
Chris Hayes Avatar answered Oct 12 '22 16:10

Chris Hayes


Instead of running

git revert --continue

Just run

git commit -m "commit message here"

If that doesn't work, make sure you've staged all the files involved in the revert. Even the files that you reverted and then discarded changes to.

like image 40
Michael Avatar answered Oct 12 '22 15:10

Michael