简体   繁体   English

恢复Git中的某些更改

[英]Reverting some changes in Git

In my Git repository I forked branch devel from branch master . 在我的Git仓库我分叉处devel方式从分支master

After this I did some changes in devel and committed a few times. 在此之后,我对devel进行了一些更改,并进行了几次。

Now I realize that some of these changes were wrong. 现在,我意识到其中一些更改是错误的。

What is the best method to revert (that is patch in the reverse direction) some (but not all) of changes (of devel relatively to master )? 还原(不是全部)某些(相对于masterdevel )更改的最佳方法是什么? I need to review each change (whether to revert it) and then how to revert the given changeset? 我需要检查每个更改(是否要还原),然后如何还原给定的变更集?

For each commit (in reverse chronological order) that has mistakes, do: 对于每个有错误的提交(按时间倒序),请执行以下操作:

git revert <sha of commit> --no-commit
git reset head

This will make your working copy reflect the repository, minus all the changes introduced in the specified commit. 这将使您的工作副本反映存储库,减去指定提交中引入的所有更改。 You just want to revert some of the changes; 您只想还原一些更改; to do this, you will want to add just those reversions to the index. 为此,您将只想将这些版本添加到索引中。 You can do this with the command line , but that can be quite cumbersome. 您可以使用命令行来执行此操作,但这可能非常麻烦。 I would recommend using a tool like Source Tree instead; 我建议改用像Source Tree这样的工具; with that, you can just visually select which hunks in which files you want to stage. 这样,您可以直观地选择要暂存的文件中的块。

Once you are satisfied that the index contains all the reversions you want, go ahead and git commit them, then do a git checkout . 一旦您满意索引包含所需的所有版本,继续进行git commit ,然后执行git checkout . to clean up your working copy. 清理您的工作副本。 Then move on to the next commit you want to (partially) revert. 然后转到您要(部分)还原的下一个提交。

Note that if your mistakes are spread over a bunch of commits and you do not want to revert each commit individually, you can pass a range of commits to git revert as well. 请注意,如果您的错误分散在一​​堆提交中,并且您不想单独还原每个提交,则也可以将一系列提交传递给git revert

Now I realize that some of these changes were wrong.... What is the best method to revert 现在,我意识到其中一些更改是错误的。...还原的最佳方法是什么

Read it all here: How to move HEAD back to a previous location? 在这里阅读所有内容: 如何将HEAD移回先前的位置? (Detached head) (独立头)

It will explain tpo you how to revert, checkout and some other useful information as well. 它将向您说明如何恢复,签出以及其他一些有用的信息。

for example: 例如:

git reflog

You can always use the reflog as well 您也可以随时使用reflog

git reflog
git checkout HEAD@{...}

This will get you back to your desired commit 这将使您回到所需的提交

在此处输入图片说明

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

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