简体   繁体   English

Github - 如何恢复某人提交?

[英]Github - How to revert someone commit ?

Someone pushed 3 commits which I want to delete, but I mean that I don't want to see them in commit history on Github, because their have wrong name.有人推送了 3 个我想删除的提交,但我的意思是我不想在 Github 的提交历史记录中看到它们,因为它们的名称错误。 How to achieve that ?如何做到这一点?

That depends on what's the current status of your repository.这取决于您的存储库的当前状态。

If those are the last three commits and you really "don't want to see them", you could reset to the commit that is located just before those three:如果这些是最后三个提交并且您真的“不想看到它们”,则可以重置为位于这三个提交之前的提交:

git reset --hard HEAD~3 git reset --hard HEAD~3

And then push using --force .然后使用--force推送

In general, if you want to revert a commit, you should use git revert .一般来说,如果你想恢复提交,你应该使用git revert For example:例如:

git revert HEAD~<2>..HEAD git revert HEAD~<2>..HEAD

That would create a commit that removes the changes from the last three commits.这将创建一个提交,从最后三个提交中删除更改。

Assuming the three commits are the three most recent commits, the first thing that will be helpful is the sum of the parent commit that you want to revert to.假设三个提交是最近的三个提交,那么首先有用的是您要恢复到的父提交的总和。 For example: 6b1d1ca .例如: 6b1d1ca You can find this on the right column of your commit history in GitHub.您可以在 GitHub 中提交历史记录的右侧栏中找到它。

Next, you want to reset your git tree to that commit using接下来,您想使用以下命令将 git 树重置为该提交

git reset --hard 6b1d1ca

Since you are removing commits from your history, you will need to force the push to GitHub, which can be done by appending the option --force .由于您要从历史记录中删除提交,因此您需要强制推送到 GitHub,这可以通过附加选项--force来完成。

git push --force

Be careful using the --force option, if you have pushed commits since as they will certainly be overwritten.小心使用--force选项,如果你推送了提交,因为它们肯定会被覆盖。

You can the rebase command to delete or change commits messages.您可以使用rebase命令删除或更改提交消息。

  • Type git rebase -i HEAD~4 .输入git rebase -i HEAD~4 The -i option does an interactive rebasing . -i选项进行交互式变基

  • Your text editor will open with the last 4 commits listed.您的文本编辑器将打开并列出最后4 个提交。 Each commit will have the word pick before it.每个提交之前都会有一个单词pick

    • Delete the lines for the commits you want to delete.删除要删除的提交的行 That will tell rebase that you don't want to keep those commits in history.这将告诉rebase您不想将这些提交保留在历史记录中。

    • Save the file and close it.保存文件并关闭它。

  • Now git should do a "rebase" of your history according to the options you chose in the file that opened, that is delete the commits you didn't want.现在 git 应该根据您在打开的文件中选择的选项对您的历史进行“rebase”,即删除您不想要的提交。

If you want to change the commits messages, change the pick to reword before each commit you want to edit and save the file.如果要更改提交消息,请在每次要编辑和保存文件的提交之前将pick更改为reword

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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