IF you have NOT pushed your changes to remote
git reset HEAD~1
Check if the working copy is clean by git status.
ELSE you have pushed your changes to remote
git revert HEAD
This command will revert/remove the local commits/change and then you can push
Answer from Jeril Kuruvila on Stack OverflowIF you have NOT pushed your changes to remote
git reset HEAD~1
Check if the working copy is clean by git status.
ELSE you have pushed your changes to remote
git revert HEAD
This command will revert/remove the local commits/change and then you can push
Actually, when you use git reset, you should refer to the commit that you are resetting to; so you would want the db0c078 commit, probably.
An easier version would be git reset --hard HEAD^, to reset to the previous commit before the current head; that way you don't have to be copying around commit IDs.
Beware when you do any git reset --hard, as you can lose any uncommitted changes you have. You might want to check git status to make sure your working copy is clean, or that you do want to blow away any changes that are there.
In addition, instead of HEAD you can use origin/master as reference, as suggested by @bdonlan in the comments: git reset --hard origin/master
How to undo pushed commits
git reset - How can I undo pushed commits using Git? - Stack Overflow
How do I get back to the most recent push/commit?
How do you revert your code back to your last git commit?
How to Undo, Revert, or Delete a Git Commit
How do I undo a merge commit?
What happens if I encounter conflicts while reverting a commit?
Videos
-
One of our artists managed to push 1 month of progress without merging
-
As you can see I tried to undo the commits one by one in reverse order
-
I couldn't undo the merge commit of Arvincle because it's empty
-
When I try to revert the commit before that I get one file I need to merge first, but the rest of the changes are not undone
Is there a way to reset the HEAD of main to the last valid commit?
You can revert individual commits with:
git revert <commit_hash>
This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits that come after that. If you want to revert a range of commits, you can do it like this:
git revert <oldest_commit_hash>..<latest_commit_hash>
It reverts all the commits after <oldest_commit_hash> up to and including <latest_commit_hash>. Some versions of git also revert the <oldest_commit_hash> itself, so double check if that commit gets reverted or not. You can always drop the latest revert commit (which reverts the oldest commit) with g reset --hard HEAD~.
To find out the hash of the commit(s) you can use git log.
Look at the git-revert man page for more information about the git revert command. Also, look at this answer for more information about reverting commits.
Extra note for re-applying reverted commits:
Usually you revert commits because you discovered the commits that you pushed turn out to have an issue. Then you first want to restore the repo to a stable state, before you continue to fix the issue. So then you would first do the git revert, as described before. Then push those revert commits, so the remote is stable. After that you would want to re-apply the reverted commits locally, so your local repo's files are back to the state before the revert. Then you can fix the issue.
To re-apply the reverted commits, you have to revert the revert commits. So what you would do, is to again execute the git revert command, but then with the range of commits of the revert commits. So your new revert commits will revert the previous revert commits. Then your files are back to the state before the first revert. Then you can fix the issue, create a new commit with the fix, and then push all the new commits.
A solution that keeps no traces of the "undo".
NOTE: Don't do this if someone already pulled your change (I would use this only on my personal repo.)
Run:
git reset <previous label or sha1>
Note: previous means the commit before the erroneous commit
This will re-checkout all the updates locally (so git status will list all updated files, meaning, the files you changed/added/.. and were committed).
Then you "do your work" and re-commit your changes (Note: This step is optional).
git commit -am "blabla"
At this moment your local tree differs from the remote
git push -f <remote-name> <branch-name>
will force the remote branch to take this push and remove the previous one (specifying remote-name and branch-name is not mandatory but is recommended to avoid updating all branches with update flag).
!! Watch-out some tags may still be pointing removed commit !! how-to-delete-a-remote-tag