如何停止在 git 中合并?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28416470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:54:35  来源:igfitidea点击:

How to stop merging in git?

gitgithubmergegit-commitabort

提问by Kamafeather

I had a three-way-merge branch merge:

我有一个三向合并分支合并:

git checkout master
git merge BranchA

>> Fast-forward merge

git merge BranchB

>> Three-way-merge (prompts for a merge commit message)

My questions are two:

我的问题有两个:

  • How can I abort the merging?With the three-way-merge I'm showed the editor where to write the merge-commit message. But if I exit without saving, gitwill anyway proceed with the merge (just the merge-commit message will be the default one instead the one I could have written but aborted)

    commit 11111233
        Merge 'BranchB' into master
    

    while, obviously not having confirmed the commit message, I would expect the merge to not happen (as the same behaviour of when I do NOT confirm a normal commit message)

  • Will the commits from the two merged branches be sorted in chronological order? Or first (in the git log) will I see the commit from the BranchA, followed THEN from the commits of the BranchB?
  • 我怎样才能中止合并?通过三向合并,我向编辑器展示了在哪里编写合并提交消息。但是如果我不保存就退出,git无论如何都会继续合并(只是合并提交消息将是默认消息,而不是我可以编写但中止的消息)

    commit 11111233
        Merge 'BranchB' into master
    

    虽然显然没有确认提交消息,但我希望合并不会发生(与我不确认正常提交消息时的行为相同)

  • 来自两个合并分支的提交会按时间顺序排序吗?或者首先(在git log)我会看到来自BranchA的提交,然后是来自BranchB的提交吗?


EDIT

编辑

To explain better the second question I made a test: 2 branches (A and B) starting from the masterbranch. I made a commit on B, then on A, then again on B, and finally again on A. Then I merged BranchAinto master. And then BranchBinto master.

为了更好地解释第二个问题,我做了一个测试:从分支开始的 2 个分支(A 和 B)。我在 B 上进行了提交,然后在 A 上进行了提交,然后在 B 上进行了一次提交,最后又在 A 上进行了一次提交。然后我将BranchA合并到了master 中。然后BranchB进入master

But when I git logon master, this is what has come out, and why I asked the second question:

但是当我git logmaster 上时,这就是结果,以及为什么我问第二个问题:

commit 730fdd4d328999c86fa4d3b6120ac856ecaccab1
Merge: 7eb581b cc1085a
Author: Arthur Conandoyle <[email protected]>
Date:   Mon Feb 9 21:24:27 2015 +0100

    Merge branch 'BranchB' into master_COPY

commit 7eb581b39a8402e1694cc4bf4eab4a3feb1143f8
Author: Arthur Conandoyle <[email protected]>
Date:   Mon Feb 9 21:23:18 2015 +0100

    BranchA) - This should be the (second) last change of the branch, and be the
    most recent into the git log, even if I merge another branch into master,
    AFTER the one where we are committing this.

commit cc1085a6aaa2ee4b26d3c3fbb93bee863d9e7c28
Author: Arthur Conandoyle <[email protected]>
Date:   Mon Feb 9 21:20:29 2015 +0100

    (BranchB) - Add settings to the last new features

commit 5f5b846a2f89886d01244ba77af941f554233b51
Author: Arthur Conandoyle <[email protected]>
Date:   Mon Feb 9 21:18:54 2015 +0100

    (BranchA) - Add some changes

commit 92a57a56b6b7c9694fbedda71b8070fc58683dde
Author: Arthur Conandoyle <[email protected]>
Date:   Mon Feb 9 21:18:17 2015 +0100

    (BranchB) - Some changes

commit 221765476e348833cf8da73b1bf5239f3d4240e8
Author: Arthur Conandoyle <[email protected]>
Date:   Tue Feb 3 12:12:19 2015 +0100

    Change (this is the last commit of the parent 'master' branch)

(As Srdjan Gruborwrote) I would have been expected first (chronologically) all the commit from the merged BranchAand THENall the commits from the merged BranchB, following the order of merging... .. but it's funny how the git loginstead show them in chronological order and the commits are not shown as grouped in branches!

(正如Srdjan Grubor所写)我本来会首先(按时间顺序)预期来自合并BranchA 的所有提交,然后是来自合并BranchB 的所有提交,按照合并的顺序......但有趣的是如何git log改为显示它们按时间顺序排列,并且提交未显示为分组在分支中!

回答by jthill

The branchB merge is perfectly ordinary, asking for an ordinary commit message. There's a default, if you quit without changing it the merge will complete. Since the merge itself has succeeded, the only way to stop it now is to supply a bad merge message (an empty one will do it absent hooks).

branchB 合并是非常普通的,要求一个普通的提交消息。有一个默认值,如果您退出而不更改它,合并将完成。由于合并本身已经成功,现在停止它的唯一方法是提供一个错误的合并消息(一个空的将在没有钩子的情况下进行)。

When a merge has stopped you can abort it with

当合并停止时,您可以中止它

git merge --abort
# or
git reset --merge

Backout for anything you just committed in error is

回退你刚刚犯的错误是

git reset --hard @{1}

There's no concept of branch "ownership" in git, all the ways you can refer to a commitare peers.

git 中没有分支“所有权”的概念,您可以引用提交的所有方式都是对等的。

You can get a better idea of the structure git logis showing you with

您可以更好地了解git log向您展示的结构

git log --graph --decorate --oneline

Try it with with --date-orderand --topo-orderand --all.

尝试使用--date-order--topo-order--all

回答by torek

Regarding your edit, git logsortsits output. This can be surprising even in a linear history. There are flags to change the sorting method, and adding --graphchanges the default sort to --topo-order.

关于您的编辑,对其输出进行git log排序。即使在线性历史中,这也可能令人惊讶。有更改排序方法的标志,添加--graph将默认排序更改为--topo-order.

The other item of note is that when git does a fast-forward of the branch, there is no merge commit:

另一个值得注意的是,当 git 执行分支的快进时,没有合并提交:

      C---D   <-- branch
     /
A---B         <-- mainline

Allowing mainlineto acquire branchvia fast-forward results in the simple graph:

允许通过简单图中的快进结果mainline获取branch

      C---D   <-- mainline, branch
     /
A---B

which is trivially drawn as a simple straight line, but forcing a merge commit (with git merge --no-ff) produces an actual two-parent merge commit:

它被简单地绘制为一条简单的直线,但强制合并提交(with git merge --no-ff)会产生实际的双父合并提交:

      C---D   <-- branch
     /     \
A---B-------M <-- mainline

which (once generalized to have more commits) presents more interesting options for the commit-sorting process.

这(曾经泛化为具有更多提交)为提交排序过程提供了更有趣的选项。

回答by Srdjan Grubor

You can "abort" the merge after you make you finish and use

完成并使用后,您可以“中止”合并

git reflog

to return to your previous tree state. Usually merges are not sorted in chronological order but in branch commit order that is then sorted separately. This means that you would see your merge commit followed by all of the source branch commits (in chronological order) which are then followed by your main branch commits (in chronological order).

返回到之前的树状态。通常合并不按时间顺序排序,而是按分支提交顺序排序,然后单独排序。这意味着您会看到您的合并提交之后是所有源分支提交(按时间顺序),然​​后是您的主分支提交(按时间顺序)。