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.)
revert to previous commit on github?
how to go back to a state at a commit?
Made an error in reverting my git commit
github - How can I rollback a git repository to a specific commit? - Stack Overflow
Can I revert multiple commits in GitHub?
How do I revert a commit directly on GitHub?
Why is the GitHub Revert button missing?
Hi, I would like to revert to a previous commit. I believe I did this correctly on my local repository using git reset --hard commit-hash. I would like to push this to github but of this results in
error: failed to push some refs to 'https://github.com/shmish/smartmark.git' hint: Updates were rejected because the tip of your current branch is behind
I am the only working on this repository, so I am not worried about conflicts, I am trying to move back one commit.
I have GitHub Desktop and I have been making regular commits. Now a bug was introduced and I want to go back and get the whole project as it was in time at a previous commit.
How to do that is not entirely intuitive. How do you go back to a previous state of the entire project at the point of a single commit?
There are no local changes according to Github Desktop....
no changesThe commit that I want to go back to is the 3rd commit down on the left. I figure that (according to their instructions) I would revert the two latest commits and I'd be back where I want to be.
a simple plan
But Github protets and claims there are changes.....
only there aren't any changes
But, there aren't any changes.
WTF Github?
I'm not worried about the code......
I'm just irritated that a versioning system so widely used would be so poorly designed.
git reset --hard <old-commit-id>
git push -f <remote-name> <branch-name>
Note: As written in comments below, Using this is dangerous in a collaborative environment: you're rewriting history
To undo the most recent commit I do this:
First:
git log
get the very latest SHA id to undo.
git revert SHA
That will create a new commit that does the exact opposite of your commit. Then you can push this new commit to bring your app to the state it was before, and your git history will show these changes accordingly.
This is good for an immediate redo of something you just committed, which I find is more often the case for me.
As Mike metioned, you can also do this:
git revert HEAD