恢复 git 中的一系列提交

发布于 2024-10-17 02:19:11 字数 408 浏览 6 评论 0原文

如何恢复 git 中的一系列提交?从查看 gitrevisions 文档,我看不出如何指定我需要的范围。例如:

A -> B -> C -> D -> E -> HEAD

我想做相当于:

git revert B-D

其中结果将是:

A -> B -> C -> D -> E -> F -> HEAD

其中 F 包含 BD 的相反内容。

How can I revert a range of commits in git? From looking at the gitrevisions documentation, I cannot see how to specify the range I need. For example:

A -> B -> C -> D -> E -> HEAD

I want to do the equivalent of:

git revert B-D

where the result would be:

A -> B -> C -> D -> E -> F -> HEAD

where F contains the reverse of B-D inclusive.

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

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

发布评论

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

评论(4

李不 2024-10-24 02:19:11

您使用什么版本的 Git?

仅在 Git1.7.2+ 中支持恢复多个提交:请参阅“使用多次恢复回滚到旧的提交。”了解更多详细信息。
当前的 git revert 手册页 仅适用于当前 Git 版本(1.7.4+)。


正如 OP Alex Spurling 在评论中报告的那样:

升级到 1.7.4 效果很好。
为了回答我自己的问题,这是我正在寻找的语法:

git revert B^..D 

B^ 表示“B 的第一个父提交”:允许在还原中包含 B
请参阅“git rev-parse 指定修订版部分”其中包括 ^,例如 HEAD^ 语法:请参阅“插入符号 ( ^) 字符的意思是?")

请注意,每个恢复的提交都是单独提交的。

Henrik N评论

git revert OLDER_COMMIT^..NEWER_COMMIT

如下所示,您可以在不立即提交的情况下恢复:

git revert -n OLDER_COMMIT^..NEWER_COMMIT
git commit -m "revert OLDER_COMMIT to NEWER_COMMIT"

What version of Git are you using?

Reverting multiple commits in only supported in Git1.7.2+: see "Rollback to an old commit using revert multiple times." for more details.
The current git revert man page is only for the current Git version (1.7.4+).


As the OP Alex Spurling reports in the comments:

Upgrading to 1.7.4 works fine.
To answer my own question, this is the syntax I was looking for:

git revert B^..D 

B^ means "the first parent commit of B": that allows to include B in the revert.
See "git rev-parse SPECIFYING REVISIONS section" which include the <rev>^, e.g. HEAD^ syntax: see more at "What does the caret (^) character mean?")

Note that each reverted commit is committed separately.

Henrik N clarifies in the comments:

git revert OLDER_COMMIT^..NEWER_COMMIT

As shown below, you can revert without committing right away:

git revert -n OLDER_COMMIT^..NEWER_COMMIT
git commit -m "revert OLDER_COMMIT to NEWER_COMMIT"
凡尘雨 2024-10-24 02:19:11

如何通过一次恢复提交来恢复一系列提交

如果您想在一次提交中恢复提交范围 B 到 D(至少在 git 版本 2 中),您可以可以这样做:

git revert -n B^..D
git commit -m "revert the commit range B to D, inclusive"

-n (或 --no-commit)参数告诉 git 恢复从 B 的父提交(排除)到 D 的提交所做的更改提交(包括),但创建任何包含已恢复更改的提交。恢复仅修改工作树(您的活动文件系统)和索引(您的暂存文件部分)。

不要忘记在运行 git revert -n 后提交更改:

git commit -m "revert the commit range B to D, inclusive"

您还可以使用相同的方法在一次提交中还原多个不相关的提交。例如:要恢复 B 和 D,但不恢复 C:

git revert -n B D
git commit -m "Revert commits B and D"

参考文献:

  1. https://www.kernel.org/pub/software/scm/git/docs/git-revert.html

  2. 感谢 Honza Haering 更正

  3. 来自man git revert

    <块引用>

    -n--no-commit

    通常该命令会自动创建一些提交,并带有提交日志消息,说明哪些提交已恢复。此标志应用将命名提交恢复到工作树和索引所需的更改,但不进行提交。此外,使用此选项时,您的索引不必与 HEAD 提交匹配。恢复是根据索引的开始状态完成的。

    当连续将多个提交的效果恢复到索引时,这非常有用。

How to revert a range of commits with one single revert commit

If you want to revert commit range B to D (at least in git version 2) in a single commit, you can do:

git revert -n B^..D
git commit -m "revert the commit range B to D, inclusive"

The -n (or --no-commit) argument tells git to revert the changes done by the commits starting from B's parent commit (excluded) to the D commit (included), but not to create any commit with the reverted changes. The revert only modifies the working tree (your active file system) and the index (your staged file section).

Don't forget to commit the changes after running git revert -n:

git commit -m "revert the commit range B to D, inclusive"

You can also revert multiple unrelated commits in a single commit, using the same method. For example: to revert B and D but not C:

git revert -n B D
git commit -m "Revert commits B and D"

References:

  1. https://www.kernel.org/pub/software/scm/git/docs/git-revert.html

  2. Thanks Honza Haering for the correction.

  3. From man git revert:

    -n, --no-commit

    Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.

    This is useful when reverting more than one commits' effect to your index in a row.

梦里°也失望 2024-10-24 02:19:11

执行 git revert OLDER_COMMIT^..NEWER_COMMIT 对我来说不起作用。

我使用了 git revert -n OLDER_COMMIT^..NEWER_COMMIT ,一切都很好。我使用的是 git 版本 1.7.9.6

Doing git revert OLDER_COMMIT^..NEWER_COMMIT didn't work for me.

I used git revert -n OLDER_COMMIT^..NEWER_COMMIT and everything is good. I'm using git version 1.7.9.6.

沙沙粒小 2024-10-24 02:19:11

使用 git rebase -i 来将相关提交压缩为一个。然后你只需恢复一次提交。

Use git rebase -i to squash the relevant commits into one. Then you just have one commit to revert.

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