GIT:如何仅恢复一项历史更改,同时保持其他更改不变?

发布于 2024-12-12 03:54:14 字数 237 浏览 0 评论 0原文

假设我已经为我的项目做出了 N 次更改并承诺了 N 次。

之后我突然发现N个改动中的一个改动导致了一个无法解决的问题。解决这个问题的唯一方法是恢复该特定更改,同时,我需要保留所有其他更改(包括此有问题的更改之前的更改和之后的更改)这个有问题的变化)就像现在一样。

如何在 GIT 中解决这个问题?我的意思是如何仅恢复一个历史更改并保留其之前和之后的所有其他更改

Say I have made changes and committed N times for my project.

After that, suddenly, I found that one change from the N changes causes a problem which has no way to solve. The only way to solve this problem is to revert that specific change back, meanwhile, I need to keep all the other changes (both the changes before this problematic change and that after this problematic change) as they are now.

How to approach this in GIT? I mean how to only revert back one historical change and keep all other changes before and after it

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

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

发布评论

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

评论(4

佞臣 2024-12-19 03:54:14

Git 社区书籍对于此类事情非常有帮助。与您相关的部分位于此处

最安全的选择是利用

$ git revert <commit-hash>

并解决任何冲突。您可以使用 git rebase 来执行此操作,但它会重写历史,并且对于已经推送或公开的存储库来说是相当灾难性的。此外,您可能希望保留显示您已进行更改且必须将其更改回来的历史记录。从长远来看,它的信息量更大。

Git community book is quite helpful with this sort of thing. The relevant section for you is here.

The safest choice is to use

$ git revert <commit-hash>

and resolve any conflicts. You could use git rebase to do this, but it rewrites history, and is quite disastrous in repositories that are already pushed or made public. Also, you may want to keep the history that shows you've made a change, and had to change it back. In the long run, it is more informative.

氛圍 2024-12-19 03:54:14

正如其他人所说,安全的做法是执行 git revert。这将清楚地表明您正在“恢复”该特定更改。它将应用反向补丁来撤消您作为参数提供的提交的影响。

另一种更致命的方法是使用 git rebase -i 并删除历史记录中的“错误提交”。这将重写该提交之后的历史记录。该特定更改的历史记录已从树中删除,但如果您已经推送并且其他人已拉取,那么如果您这样做,他们将看到合并冲突。

Like others have said, the safe thing to do is to do git revert. This will make it clear that you're "revert"ing that specific change. It will apply a reverse patch to undo the effects of the commit you've provided as an argument.

The other more deadly way is to use git rebase -i and drop the "bad commit" in the history. This will rewrite the history from that commit onwards. History of that specific change is excised from the tree but if you've already pushed and others pulled, they will see merge conflicts if you do this.

请叫√我孤独 2024-12-19 03:54:14

使用 git 恢复。这将创建一个新的提交,其中已恢复的提交的所有更改都将被恢复。这样就保留了历史。

Use git revert. This creates a new commit, where all changes of the reverted commit are reverted. This preserves the history.

乖不如嘢 2024-12-19 03:54:14

请注意,如果您有多个更改需要还原,您还可以避免为要还原到的每个提交打开编辑器:

git revert 手册页

--no-edit

使用此选项,“git revert”将不会启动提交消息编辑器。

这使得编写脚本和链接多个恢复变得更加容易。

Note that if you had more than one change to revert, you also can avoid the editor being opened for each commit you are reverting to:

git revert man page:

--no-edit

With this option, 'git revert' will not start the commit message editor.

That makes scripting and chaining multiple revert easier.

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