I am a newbie in the GitHub world. I've been working on a project for my coding bootcamp. I had everything working just right to the specifications of the project and I was all done. Then I foolishly seem to have committed an old early version of the project, thus covering over the final version. I have tried to revert the last commit from the GitHub website, but the interface does not seem to follow the instructions. I tried the "git revert {commit#}'" command from my terminal, but that did not work either. I need suggestions. I'd like to get it done from the command line if possible.
-
1What's the error you are getting when tried `git revert ...` in terminal? – Prathap Reddy Aug 24 '20 at 19:00
-
Describe what error you are getting when you revert. And probably you can check is conflicts doesn't exist. – Nandish Aug 24 '20 at 21:41
1 Answers
git revert
commits a reverse change, so from the history point of view you will have two unnecessary commits that cancel each other. The {commit#}
in your case should be the ID of the commit that you want to undo (= the last one). This should work as long as there are no other commits on top of it, otherwise you might get conflicts which require more work.
If you don't have any other commits apart from the one you want to undo, there is also a better way - simply move the branch back to point to the last commit you want to keep (= one before last).
Something like this (I assume you are working on master
, that you didn't do revert
yet and that there are no other people involved):
git checkout -b tmp_branch master~1
git branch -f master tmp_branch
git checkout master
git branch -D tmp_branch
git push -f origin master
And voilà. If your master
is protected in GitHub, you will have to unprotect it. You can repeat this to go further back (or just use ~2
, ~3
etc.)

- 4,436
- 2
- 12
- 20
-
1Thanks so much @jurez. This was my first time asking a question in Stack Overflow. I appreciate the quick response and step-by-step instructions. Thanks for your contribution! – Doug Davidoff Aug 24 '20 at 19:29
-
@DougDavidoff If the answer helped, consider marking it as accepted so that other users can see that the issue has been resolved. – qwerty Aug 24 '20 at 20:43