'How to stop merging in git?

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, git will 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?

EDIT

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

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

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 Grubor wrote) I would have been expected first (chronologically) all the commit from the merged BranchA and THEN all the commits from the merged BranchB, following the order of merging... .. but it's funny how the git log instead show them in chronological order and the commits are not shown as grouped in branches!



Solution 1:[1]

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), edit: or have an editor that can error out like vim's :cq that I just learned about.

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 commit are peers.

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

git log --graph --decorate --oneline

Try it with with --date-order and --topo-order and --all.

Solution 2:[2]

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

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

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

Allowing mainline to acquire branch via fast-forward results in the simple graph:

      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:

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

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

Solution 3:[3]

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

Solution 4:[4]

This command will help you to abort a local merging:

git merge --abort

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 torek
Solution 3 Srdjan Grubor
Solution 4 Sambit Das