恢复较早的提交的一部分

发布于 2025-02-03 10:28:25 字数 114 浏览 3 评论 0原文

我知道我缺少一些明显的东西,但是...假设我有3个承诺会影响相同的两个文件:a和b。我想恢复第二次提交中A中所做的更改,但我不想失去第三个提交中b的更改。如果我还原然后推荐,这不会在第三次提交中覆盖我对B的新更改?

I know I'm missing something obvious but... let's say I have 3 commits affecting the same two files: a and b. I want to revert the changes made in the 2nd commit to a, but I don't want to lose the changes made in the third commit to b. If I revert and then recommit, won't that overwrite my new changes for b in the third commit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是伱的 2025-02-10 10:28:25

tl; dr:听起来您打算做 partial commit 2的偏移,如果您这样做,则 no ,file <代码> B 将根本不更改。

详细信息:基于问题和评论,我们知道:

  1. 所有3个提交都会修改两个文件ab
  2. 您希望恢复 commit 2,但基于评论,该提交中只有文件 a :

如果我还原提交2并修改A(仅),然后提交更改

那么重要的是要意识到git还原只是一种便利和约定,您可以准确地执行恢复如果您愿意,可以手动。无论您是使用还原命令还是手动执行,最终结果将是一个新提交(我们称其为“提交4”),它揭示了以前的提交的结果。在您的情况下,您要撤消Commit 2的A 部分,因此您可以手动撤消文件a的更改,然后手动创建commit 4。我假设,如果您手动进行操作,则不会指望文件b会受到任何影响。

您也可以使用还原命令来帮助您制作提交4,例如:

# revert commit 2 but don't commit it yet
git revert <commit-2-hash> --no-commit

# unstage file b so that only the change to a is reverted
git reset b

# restore file b so that it is no longer modified
git restore b

# commit the change
git commit # Adjust the commit message appropriately

如果您使用还原命令,则GIT提出了有用的提交消息。但是,如果您以某种方式修改恢复,我建议修改这样的提交消息:

Partial Revert "Previous commit message..."

This commit only reverts the changes to file a.

This reverts commit <commit-2-hash>.

附加说明:如果执行常规还原而不是上述部分还原,则更改文件<代码> b COMM 2中的代码也将被撤消。请参阅 bk2204的答案有关此方法的工作方式的更多详细信息。

tl;dr: It sounds like you intend to do a partial revert of commit 2, and if you do that, then No, file b will not be changed at all.

Details: Based on the question and the comments, we know:

  1. All 3 commits modify both files a and b.
  2. You wish to revert commit 2, but only file a in that commit, based on the comment:

If I revert commit 2 and modify a (only) and then commit the changes

It's important to realize that git revert is merely a convenience and a convention, and you could do exactly what revert does manually, if you wanted to. Regardless of whether you use the revert command or do it manually, the end result will be a new commit, (let's call it commit 4), that undoes the result of a previous commit. In your case, you want to undo a portion of commit 2, so you could manually undo the change of file a and create commit 4 manually. I assume if you did it manually you would not expect file b to be affected in any way.

You could also use the revert command to assist you with making commit 4, for example:

# revert commit 2 but don't commit it yet
git revert <commit-2-hash> --no-commit

# unstage file b so that only the change to a is reverted
git reset b

# restore file b so that it is no longer modified
git restore b

# commit the change
git commit # Adjust the commit message appropriately

If you use the revert command, Git proposes a useful commit message. However, if you modify the revert in some way, I recommend modifying the commit message like this:

Partial Revert "Previous commit message..."

This commit only reverts the changes to file a.

This reverts commit <commit-2-hash>.

Additional Note: if you do a regular revert instead of the partial revert described above, then the changes to file b in commit 2 would also be undone. See bk2204's answer for more details about how this works.

傻比既视感 2025-02-10 10:28:25

当Git执行恢复时,它实际上将其用于该提交并将其倒转,然后将其应用于工作树和索引。它使用与樱桃挑选的机械以及引擎盖下的某些形式的重击,并且工作非常相似。

如果您在恢复中所做的更改位于文件的不同部分与您在Commit 3中所做的更改相比,将恢复清洁,您将进行提交1和3的更改,而不是2。如果恢复的变化与提交3的更改重叠(或几乎重叠),然后您可能会发生冲突,您必须解决继续下去。在任何情况下,git都不会简单地撤消提交3中的更改3,因为您要求恢复commit2。

这就是为什么当您按顺序恢复多个提交时,您想在早期的较早之前恢复较晚的提交,因为这会消除他们最初完成的顺序变化,最大程度地减少了冲突的风险。

When Git performs a revert, it essentially takes the patch for that commit and reverses it, then applies it to the working tree and index. It uses the same machinery as cherry-pick and some forms of rebase under the hood to do so and works very similarly.

If the changes you're making in the revert are in different parts of the file from the changes you made in commit 3, then the revert will apply cleanly, and you will have the changes from commits 1 and 3 but not 2. If the changes in your revert overlap (or nearly overlap) with the changes in commit 3, then you'll probably get a conflict, which you'll have to resolve to continue. In no case is Git going to simply undo the changes in commit 3 because you asked to revert commit 2.

This is why when you are reverting multiple commits in sequence, you want to revert the later ones before the earlier ones, because that undoes the changes in the order they were originally done, minimizing the risk of conflicts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文