Git
简体中文 ▾ Topics ▾ Latest version ▾ git-revert last updated in 2.44.0

名称

git-revert - 还原某些现有提交

概述

git revert [--[no-]edit] [-n] [-m <父提交数量>] [-s] [-S[<键 ID>]] <提交>…​
git revert (--continue | --skip | --abort | --quit)

描述

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).

Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset[1], particularly the --hard option. If you want to extract specific files as they were in another commit, you should see git-restore[1], specifically the --source option. Take care with these alternatives as both will discard uncommitted changes in your working directory.

关于这三个命令的区别,见git[1]中的 "重置、恢复和还原"。

选项

<提交>…​

Commits to revert. For a more complete list of ways to spell commit names, see gitrevisions[7]. Sets of commits can also be given but no traversal is done by default, see git-rev-list[1] and its --no-walk option.

-e
--edit

有了这个选项,git revert 会让你在提交之前编辑提交信息。如果从终端运行该命令,默认情况下也是如此。

-m 父编号
--mainline parent-number

Usually you cannot revert a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent.

Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want.

更多详情,请参阅 revert-a-faulty-merge 如何操作

--no-edit

使用该选项后,git revert 将不会启动提交信息编辑器。

--cleanup=<模式>

这个选项决定了提交信息在传递给提交机制之前将如何进行清理。更多细节见 git-commit[1]。特别是,如果 <模式> 的值为 scissors,那么在发生冲突时,scissors 将被附加到 MERGE_MSG 上。

-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.

这在连续恢复多个提交对索引的影响时非常有用。

-S[<keyid>]
--gpg-sign[=<键 ID>]
--no-gpg-sign

GPG 签名提交。keyid 参数是可选的,默认为提交者身份;如果指定了,则必须与选项相连,不加空格。--no-gpg-sign 用于还原 commit.gpgSign 配置变量和先前的 --gpg-sign

-s
--signoff

Add a Signed-off-by trailer at the end of the commit message. See the signoff option in git-commit[1] for more information.

--strategy=<策略>

Use the given merge strategy. Should only be used once. See the MERGE STRATEGIES section in git-merge[1] for details.

-X<选项>
--strategy-option=<选项>

Pass the merge strategy-specific option through to the merge strategy. See git-merge[1] for details.

--rerere-autoupdate
--no-rerere-autoupdate

在 rerere 机制重用当前冲突的记录解析来更新工作树中的文件后,允许它也用解析的结果来更新索引。 --no-rerere-autoupdate`是一个很好的方法,在用单独的 `git add 提交结果到索引之前,可以反复检查 rerere 所做的事情,并抓住潜在的错误合并。

--reference

Instead of starting the body of the log message with "This reverts <full-object-name-of-the-commit-being-reverted>.", refer to the commit using "--pretty=reference" format (cf. git-log[1]). The revert.reference configuration variable can be used to enable this option by default.

序列器子命令

--continue

使用`.git/sequencer`中的信息继续进行中的操作。 可以用来在解决拣选或还原失败的冲突后继续执行。

--skip

跳过当前的提交,继续进行队列中其余的命令。

--quit

忽略当前正在进行的操作。 可以用来在拣选或还原失败之后清除序列器的状态。

--abort

取消操作并返回到预排序状态。

实例

git revert HEAD~3

还原 HEAD 中倒数第四个提交指定的更改,并创建一个包含还原更改的新提交。

git revert -n master~5..master~2

将 master 中倒数第五次提交(包含)到 master 中倒数第三次提交(包含)的改动还原,但不创建任何包含还原改动的提交。还原只会修改工作区和索引。

讨论

While git creates a basic commit message automatically, it is strongly recommended to explain why the original commit is being reverted. In addition, repeatedly reverting reverts will result in increasingly unwieldy subject lines, for example Reapply "Reapply "<original-subject>"". Please consider rewording these to be shorter and more unique.

配置

Warning

Missing zh_HANS-CN/includes/cmd-config-section-all.txt

See original version for this content.

Warning

Missing zh_HANS-CN/config/revert.txt

See original version for this content.

GIT

属于 git[1] 文档

scroll-to-top