文章

Git使用技巧记录

revert

Revert 一条merge提交,会报错 no -m option was given.

1
2
3
ramboq-demo|dev ⇒ git revert 2f7400a9d7725594a8843cccba26db2f74fe1fd7
error: commit 2f7400a9d7725594a8843cccba26db2f74fe1fd7 is a merge but no -m option was given.
fatal: revert failed

查询了下-m参数的解释

1
2
3
4
5
6
7
8
-m parent-number, --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.

  See the revert-a-faulty-merge How-To[1] for more details.

大致意思就是merge提交有两个节点,回滚操作是回滚到哪个节点上的意思

摘抄What are parents on a merge commit?翻译过来

父节点Commit parents

一个提交的父节点是当前提交项前面最顶头的一项git提交,比如下面介绍:

1
2
3
4
5
  C3 *
     |
  C2 *
     |
  C1 *

C2 是 C3的父节点, C1是 C2的父节点。

合并提交节点Merge commit

一个合并提交节点,不像其他提交,他是有两个父节点的,如下:

1
2
3
4
5
6
7
8
9
10
11
                 M1  * (Change File1 and Change File2)
                     | \
  (Change File1) A5  *   * B3 (Change File2)
                     |   |
                 A4  *   * B2
                     |   |
                 A3  *   * B1
                     |  /
                 A2  *
                     |
                 A1  *

B分支是从A2检查出来的,合并进A分支,产生M1合并节点。在这个例子中,M1就有两个父节点。

  • 父节点Parent 1: A5 (Commit changed File1)
  • 父节点Parent 2: B3 (Commit changed File2)

回退M1,你想保持为A分支,就是想把B分支的改动回退掉,所以父节点应该取值1

1
$ git revert M1 -m 1

参考文章

  1. stackoverflow.com

  2. What are parents on a merge commit?

本文由作者按照 CC BY 4.0 进行授权