123

It seems like you can do just about everything else directly on GitHub using the web interface, like edit and create and delete files, but I am unable to find a way to revert a commit, like you can in the GitHub local apps (Windows, and Mac) and like you can do on the git command line.

I'm just wondering am I just missing something. Is the revert button hidden?

I tried searching online and came across something that seemed to suggest that this was to prevent synchronization errors when working with lots of people. Is this correct?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
phocks
  • 2,933
  • 5
  • 27
  • 28

4 Answers4

65

No, that feature isn't directly available on the GitHub web interface (as opposed to the "Revert" button recently added for GitHub for Mac/Windows)

Actually, it is for pull requests only, since June 24th, 2014:

Introducing the Revert Button

you can easily revert a pull request on GitHub by clicking Revert:

https://camo.githubusercontent.com/0d3350caf2bb1cba53123ffeafc00ca702b1b164/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6c696e6b2e706e67

You'll be prompted to create a new pull request with the reverted changes:

https://camo.githubusercontent.com/973efae3cc2764fc1353885a6a45b9a518d9b78b/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f68656c702f70756c6c5f72657175657374732f7265766572742d70756c6c2d726571756573742d6e65772d70722e706e67


git revert is a bit more complex to manage through the web as it can accept a range of commits.
It shouldn't be an issue in terms of collaboration though: a revert adds a new commit, it doesn't change the history of existing commits.

Pang
  • 9,564
  • 146
  • 81
  • 122
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @OlegKislitsyn yes, but for pull requests only, not just for any commit. I have edited the answer to add that feature, but it isn't an exact fit for the OP. – VonC Sep 05 '14 at 18:18
  • I can't find the Revert button anywhere on the current GitHub workflow? – Petrus Theron May 23 '16 at 08:21
  • @pate It should be visible to the maintainer (the one managing the original github repo to which others are making PR -- Pull Requests) – VonC May 23 '16 at 08:22
  • Still can't find it. Sample URL? This is a repo in an org, and I am the owner of the repo, so I should be able see it :/. – Petrus Theron May 23 '16 at 08:31
  • @pate All I have is the official help: https://help.github.com/articles/reverting-a-pull-request/ – VonC May 23 '16 at 08:32
  • Thanks, my issue was that I didn't use a pull request. – Petrus Theron May 23 '16 at 12:18
  • @pate OK, that makes sense. – VonC May 23 '16 at 12:26
  • 15
    Geez, this is a nightmare. I saved a few broken versions before I caught the error, and I need to trash them, they're garbage and I don't want them around causing trouble at all. And apparently, I'm stuck with them. Great system. – John Smith Apr 04 '17 at 17:55
  • @MichaelKupietz No: if you are the only one working on this repo, you can do an interactive rebase locally, drop the commits you don't want (or squash them) and push --force. – VonC Apr 04 '17 at 17:56
  • 15
    Sorry, I don't know what most of those words mean. Moving to GitHub was probably a bad idea, I just wanted to post my code publicly and track versions as I worked on it, but this feels like trying to use a howitzer to swat a fly. I've been googling for about an hour now trying to solve this "problem", I can't understand why it's this hard. – John Smith Apr 04 '17 at 17:59
  • @MichaelKupietz Yes, those operations are better done on a local clone (on your computer), like a rebase (https://www.ibm.com/support/knowledgecenter/SSSH27_9.0.0/com.ibm.rational.clearcase.cc_admin.doc/topics/c_backup.htm). See also http://learngitbranching.js.org/ – VonC Apr 04 '17 at 18:02
  • 16
    Ouch! I'll have take a whole tutorial, which involves diving down into the command line, just to get rid of a simple change to a text file? (And that IBM page seems to be about a completely unrelated topic, near as I can tell? Some sort of proprietary backup system documentation?) I appreciate the effort to help, but I don't think Github can possibly be for what I thought it was for, it just seems too counterproductive. I just wanted to open-source my code & save subsequent versions as I work. But not at cost of needing an engineering degree to accomplish simple tasks like reverting a mistake. – John Smith Apr 04 '17 at 19:09
  • @MichaelKupietz sorry for the IBM link (wrong copy-paste) I meant https://marklodato.github.io/visual-git-guide/index-en.html#rebase. And the tutorial is indeed about the command line, but more importantly about helping you understand the concepts behind Git. That being said, I understand your frustration. Revert on GitHub will create a new commit, which cancel an older one. But it is best done on a local clone rather than through the web interface: http://stackoverflow.com/a/35386185/6309 – VonC Apr 04 '17 at 19:16
  • 4
    This whole thing is too complicated. I feel like I need TextEdit and all there is is Emacs. I don't want to learn a whole new lingo and set of abstract concepts, I just wanted to share my code on a popular, accessible platform. Gonna bail now, the site is kvetching that this should go into chat & that wasn't my intent. I appreciate the attempts to help. – John Smith Apr 04 '17 at 19:22
  • @MichaelKupietz Another option, if the history is not too important, is simply to rename or delete your current repo, and make a new one in which you can upload your content. – VonC Apr 04 '17 at 19:28
  • Thanks, I did consider that, but part of the appeal was having version history, trashing it completely would defeat the purpose. I ended up installing the desktop client, which at least allows you to "pseudo-revert" by adding new commits that are reversions to old ones. Not a great answer, but it'll tide me over until I can find a more appropriate way to host work-in-progress code. – John Smith Apr 07 '17 at 06:34
  • 11
    GitHub needs to add this feature. Why isn't it already there?? – TheTechRobo the Nerd May 02 '20 at 17:03
  • If the change you need to revert is small, its pretty easy in the GitHub UI to manually edit the changes to remove them. You can even look at a document in the history and find the version you want it to be, view it, copy it and paste it over the latest version in the editor. Its a pain but occasionally useful. – Lee Meador Feb 04 '21 at 17:55
  • This should NOT be the accepted solution. The one below it by @Zia Ullah should be. Two lines of code, and it's DONE. – Forklift17 Feb 24 '21 at 01:56
18

If you want to use just github web. There is a tedious way though.

Step 1. Goto commit history, find the commit hash which you want to revert to; and click "Browse repo at this point in history"

Step 2. Create a new branch from this commit hash (say "temp")

Step 3. Delete the branch which had the problem (say "main")

Step 4. Goto "temp" branch and create "main" branch from it. And you're done.

Ofcourse, this is not a good way and it might only work for recently created commits.

vohrahul
  • 1,123
  • 10
  • 17
  • 4
    "Revert a commit" is different from "Revert to a commit" Your answer is a nice way for "Revert to a commit" – Timothy.Li Apr 13 '22 at 08:34
  • 1
    Theoretically, reverting to n-1 commit, is same as reverting n commit. No? I know the answer is a different approach, but it still helps I guess. But, I agree with you. – vohrahul May 06 '22 at 14:31
  • 3
    Not quite. Reverting to `n-1` commit (in git this is actually called "reseting to") means deleting the `n` commit as if it never happened. Reverting the `n` commit means creating a new `n+1` commit that makes all the opposite changes that `n` commit did. – Marko May 24 '22 at 11:16
  • 1
    I agree this is not a fun way to do it, but still appreciate this answer. – CubicInfinity Mar 11 '23 at 16:48
  • I can't imagine this approach works well in any nontrivial team environment. "Hey guys, where'd the `main` branch go?". Or even worse, "where'd `main` and all the commits I just pushed to it go?". – aroth Aug 23 '23 at 11:18
9

You can't revert back to that commit on the GitHub web site, there is no option to do this action, Instead, you should use command-line or GitHub Desktop as below

enter image description here

After REVERT, do not forget to PUSH the code. For more details, check the link How to restore deleted files on GitHub website?

Arsman Ahmad
  • 2,000
  • 1
  • 26
  • 34
Mohamed
  • 806
  • 13
  • 30
3

This worked for me (GITHUB).

 1) git reset --hard HEAD^   
 2) git push origin -f

Note:- These lines reverts commit back one by one.

Zia Ullah
  • 315
  • 3
  • 11
  • 16
    This is actually something else than reverting a commit - this is discarding it completely (rewrites history). – Jan Mares Nov 23 '20 at 18:29
  • This should be marked as the accepted answer. – Forklift17 Feb 24 '21 at 01:54
  • 3
    If you are publishing to a remote repo that other people will be using you should think twice before using this. This is not reverting, this overwriting and is pretty dangerous. Definitely very seldom the correct answer (maybe only for a personal project). – keji Mar 30 '21 at 14:01
  • glad to know that it helped. – Zia Ullah Mar 30 '21 at 17:51
  • 11
    This should definitely not be marked as the answer. "Revert" means something specific in git and this answer does not do that. The very fact that the OP is using GitHub means it's a shared repo. Using the commands in this answer would have nasty side-effects. And the OP is asking for a button in the UI, not a git command. – norepro Mar 30 '21 at 20:52
  • This is not what reverting means in Git, and can really mess up a shared repository. -1 – Maximillian Laumeister Oct 03 '21 at 23:48
  • Agree this must not be an accepted answer, but I think it is a valid alternative if you know what you're doing. Just would be good to add a warning about force pushing into the remote and a few notes on what each command does. Actually, +1 – RAM237 Aug 05 '22 at 17:04
  • This can't be the accepted answer since it ignores the fact that it is CLI instead of GitHub UI – shaedrich Jul 07 '23 at 13:53