39

I'm using git. I did a normal merge, but it keeps asking this:

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.

And even if I write something, I can't exit from here. I can't find docs explaining this. How should I do?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Luca Reghellin
  • 659
  • 2
  • 7
  • 13

8 Answers8

37

This is depend on the editor you're using.

If you can use ESC and :wq or ESC and Shift+zz. Both command save file and exit.

You also can check ~/.gitconfig for editor, in my case (cat ~/.gitconfig):

[user]
    name = somename
    email = [email protected]
[core]
    editor = vim
    excludesfile = /home/mypath/.gitignore_global
[color]
  ui = auto
  # other settings here
Roman Kiselenko
  • 553
  • 4
  • 7
5

The answer marked correct here did not work for me as after you quit the editor with wq or :q!, the file gets saved and the merge happens.

The alternative, I've found for now is to suspend the vim process by using a Ctrl + Z and then aborting the merge with a git merge --abort and then killing the background process (you'll see the PID when you do the Ctrl + Z)

FargolK
  • 1,629
  • 1
  • 12
  • 20
mrsauravsahu
  • 151
  • 1
  • 2
4

I did not find the answer to the question here if you happen not to be using the vim editor. My editor for WSL appeared to be nano and the only way I could get out of the MERGE_MSG screen was to use: CTRL+X.

Hope this helps someone else.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Andrew
  • 41
  • 1
  • Exiting nano in this way still continues with the git merge. I am currently looking for a way to avoid that - exit nano and abort the merge. – KenB Nov 20 '20 at 22:11
1

I know this may be late, however I have noticed that it only occurs if I make a commit to a branch is a bit more updated than the branch that i'm merging.

Image to help with understanding

It turns out that if I change a branch (i.e., add more files to the branch), and try and merge another branch into it, Git will request me to write a MERGE_MSG for that branch.

In this image, it shows that I added a LICENSE file in master and a .gitignore file in branch npm-init.

Because master contains a new file, LICENSE, and npm-init does not know about it, Git asked me to write a message about why I should merge it.

Similarly, I merged add-eslint into add-dev before I merged the changes in install-deps.


I know this may be hard to understand (even for me at first), but I hope the image clears things up :)

1

It depends on your editor. In vi and vi forks like vim, exiting by either saving (wq) or force exiting without saving (q!) will NOT prevent merge to continue. In order to stop merge to continue, you need to exit vim with an error.

Exit vim using cq! will prevent merge to happen.

CodeTalker
  • 111
  • 2
1

Also late to this but to build on @CodeTalker's answer:

If you are using vi or vim, the :help cquit documentation in vim states

:cq[uit][!] {N}     Quit Vim with error code {N}.  {N} defaults to one.
            Useful when Vim is called from another program:
            e.g., a compiler will not compile the same file again,
            `git commit` will abort the committing process, `fc`
            (built-in for shells like bash and zsh) will not
            execute the command, etc.
            {N} can also be zero, in which case Vim exits
            normally.
            WARNING: All changes in files are lost!  Also when the
            [!] is not used.  It works like ":qall!" |:qall|,
            except that Vim returns a non-zero exit code.

note the call out for git commit in that snippet

Sparrow1029
  • 268
  • 2
  • 9
0

Instead of using the 'git commit' command on the git bash, you could use the 'git commit --m (Then the quoted description of the commit message).

My stance on this is as a result of my inability to exit the commit editor after getting there. Any contrary opinion is welcome anyways.

0

Just open new git bash window in the same location and write the

$ git commit -m "<a commit message to explain why this merge is necessary>"

and this will finish the merge to your local branch.

After that you can push your local commit to the remote.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Sven
  • 1