简体   繁体   中英

How can I revert a push to repository in GitHub

I commited few changes, and made a push to remote branch in github. But then I realized that I did some mistakes in the commit, and pushed lot of wrong files. Is there a way to revert the push?

The git revert command does not rewrite history, but does take away the changes from a commit with a new commit. Then all you would have to do is to push again. This is the suggested method.

If you really want to make github look like a particular commit never really happened, there is a way, but use caution (especially if other users contribute to the github repository). In fact if others use this github repository, stop now and use the method mentioned in the first paragraph.

If this is your own private github repository and the last push is what you need to take away (assumes you are still on the same local branch):

git reset --hard HEAD~
git push -f

If it is not the last push, see man pages for either git cherry-pick , or git rebase to get your local directory to match what you want github to look like before doing the git push -f . If you do not supply the -f option to git push , you will not rewrite history. And any push will fail if it attempts to rewrite history. The -f option should not be used by default.

Keep in mind that once you have pushed your commits to a remote repository that you should exercise caution, especially so if you have some intention of rewriting history (ie removing all your undesired commits by rebasing).

If you do not care about history, then reverting your bad changes can be done by using git revert and passing it the commit identifiers you want to undo.

For example, if you have changes contained in two commits, A and B, you would like to remove:

git revert A B

This will revert commits A and B, creating a commit for each.

By default, Git will create additional commits on top of HEAD to illustrate changes being reverted. Check out the manual page for git revert on further details on how to use it.

After making the reverts you need, simply push up again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM