Home > Back-end >  How to undo the last commit on GitHub?
How to undo the last commit on GitHub?

Time:11-15

I want to undo the last commit pushed to GitHub on the master branch of a repository, and make it so that it's as if this commit never existed and doesn't appear in the commit history.

How do I go about this?

Note to those voting to close — the proposed alternative questions are needlessly complex, have condescending answers, and are littered with giant walls of text that are difficult to sift through.

Hence, this simple question with a simple answer for my benefit and that of posterity.

CodePudding user response:

I think you're looking for the --force argument.

You can reset to a specific commit with git reset --hard {commit sha} and then force the change to origin with git push --force.

Note that if others are using your repo, they will need to use git pull --force or they may inadvertently put your unwanted commit back into the repo.

I recommend reading the help documentation for git reset and git push before taking action.

CodePudding user response:

If you wish to rewind the master branch of the repository to a previous pushed commit, simply run this command — of course, with the appropriate commit hash:

git reset --hard a0b1c2d3e4f5g6h7i8j9k0l1m2n3o4p5q6r7s8t9
git push --force

This may have unintended consequences if, for instance, there are collaborators on your repository. But, I am sure you know what you're doing

  • Related