49

Every time I run a git merge command it opens the text editor asking me to add an extra message.

How can I stop git from opening the editor & simply merging my branches? Because when it opens the editor it doesn't complete the merge, even if I add an extra message & save the file, the terminal just hangs on my git merge command.

Merge branch 'my-feature-branch' into main-development

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
# 
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Holly
  • 7,462
  • 23
  • 86
  • 140
  • 4
    possible duplicate of [Git merge doesn't use default merge message, opens editor with default message](http://stackoverflow.com/questions/12752288/git-merge-doesnt-use-default-merge-message-opens-editor-with-default-message) – dtech Jul 07 '14 at 11:09
  • 1
    "when it opens the editor it doesn't complete the merge, even if I add an extra message & save the file, the terminal just hangs on my git merge command." This is likely a bigger problem. When you do a non-merge commit do you get the same behaviour? – ChrisGPT was on strike Jul 07 '14 at 12:42
  • @dtech it's similar but not the same, same same but different :) – Holly Jul 10 '14 at 08:54

3 Answers3

49

Use the --no-edit option, you can read about it in the documentation.

Note that using the default message is discuraged, since it provides no meaningful information about the changes introduced with this merge.


On a sidenote: To continue merging you probably have to close the editor.


If you have a git version prior to 1.7.8 there is still a way to achieve what you want by using the env command.

env GIT_EDITOR=: git merge <ref-you-want-to-merge>

For easier usage you could create an alias.

git config --global alias.merge-no-edit '!env GIT_EDITOR=: git merge'

Which then can be used using git merge-no-edit <ref-you-want-to-merge>.

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
  • Do you know what version of git this was added to - we have CentOS 6 servers running git 1.7.1 and they do not support the `--no-edit` option. – Hamish Downer Oct 21 '15 at 17:14
  • 1
    @HamishDowner while it seems that there are no particular release notes which introduce the `--no-edit` option, [it's mentioned in the `1.7.10` release notes](https://github.com/git/git/blob/8d530c4d64ffcc853889f7b385f554d53db375ed/Documentation/RelNotes/1.7.10.txt#L23-L26) saying that it can be used with "Git version 1.7.8 or newer". I'll edit my answer to include a solution for earlier versions. – Sascha Wolf Oct 21 '15 at 18:46
  • Just found that the [1.7.8 release notes](https://github.com/git/git/blob/8d530c4d64ffcc853889f7b385f554d53db375ed/Documentation/RelNotes/1.7.8.txt#L85) mention the `--edit` option, so that implies the --no-edit option. – Hamish Downer Oct 22 '15 at 07:56
  • 2
    The correct way is to do `GIT_MERGE_AUTOEDIT=no git merge ` instead of unsetting `GIT_EDITOR` [see this](https://git-scm.com/docs/merge-options#merge-options---no-edit). – hIpPy May 23 '17 at 23:21
17

You can use

git merge --no-edit

This is the man page :

--edit, -e, --no-edit Invoke an editor before committing successful mechanical merge to further edit the auto-generated merge message, so that the user can explain and justify the merge. The --no-edit option can be used to accept the auto-generated message (this is generally discouraged). The --edit (or -e) option is still useful if you are giving a draft message with the -m option from the command line and want to edit it in the editor.

Older scripts may depend on the historical behaviour of not allowing the user to edit the merge log message. They will see an editor opened when they run git merge. To make it easier to adjust such scripts to the updated behaviour, the environment variable GIT_MERGE_AUTOEDIT can be set to no at the beginning of them.

Fredszaq
  • 3,091
  • 1
  • 18
  • 20
  • 1
    NOTE: To merge another branch or ref into the current branch, the branch name must come *after* the `--no-edit` option like this: `git merge --no-edit origin/my-feautre-branch` -- The order of these arguments matters. The editor will still open if passing the ref first. – John Kary Aug 29 '22 at 18:38
11

Add the following line into your .bash_profile, .bashrc or .zshrc file:

export GIT_MERGE_AUTOEDIT=no
Flavio Wuensche
  • 9,460
  • 1
  • 57
  • 54