Fix: Please enter a commit message to explain why this merge is necessary

Possibly one of the most embarrassing error messages to get stuck in when using the git development hub involves commit messages after doing a normal merge. You might see a line instructing you to please enter a commit message to explain why this merge is necessary, but if you write anything there it doesn’t allow you to exit. The reason this is so embarrassing is because this isn’t an error message at all.

The prompt is actually there for humans who have to edit git merges, and any text you add in is simply a notice ot other developers who might be looking at what you wrote. You have to exit the editor the same way you would if you were using it from a standard terminal screen.

Exit git Merges that Ask for Commit Messages

Generally, you’ll see this if you’ve performed a normal merge on git as part of a separate document. Sometimes you might get this prompt after a block of code when you merge an updated upstream into its own topic branch. None of the documentation from git explains what to do when you reach it because it genuinely isn’t a prompt at all.

Both lines of this message start with the # symbol, which means that they’re comments. Since git doesn’t know how many developers are working on a project, this is added so you can leave a message to others about why you merged an upstream into a topic branch. You need to know which editor you’re using to exit it, however.

In a majority of cases, you’re using the vi or vim editors. Push the Esc key then type :wq and push enter to exit. This is the same way that you’d exit from vim on any other occasion. This saves the file and then exits, which will take you right out of git.

Notice any cues that might help you to know what kind of editor you’re using. In our example screenshot, the extra tilde characters showing new lines yet to be inserted into a file were a telltale giveaway that the git platform put us into vim. Then again, if you can’t figure out what editor you’re using, push Esc and use the :wq command because vi and vim are almost universal at this point so you’ll find this will usually get you out.

On the other hand, if you were using nano then simply push Ctrl+X and type y when asked whether to save changes. As soon as you push enter, you’ll be out of git and back wherever you were beforehand. You’ll generally notice a line at the top of the terminal that reads “GNU nano” in these cases. If not, then look for a number of keyboard shortcuts at the bottom of the window.

If you find that neither of these methods work, then push Ctrl+X followed by Ctrl+C to exit. This should get you out of git on the off chance that you were using the emacs editor. This is an unusual circumstance. In most cases, Esc followed by :wq should work and Ctrl+X followed by y should work in cases where that doesn’t. You’ll want to use Ctrl+X followed by Ctrl+C only if you either know you’re in emacs or if these two methods don’t work. Ctrl+C should also work, albeit without saving, if you’re using the JOE editor at your git terminal.

Once you’re out and back at the prompt, type cat ~/.gitconfig | grep editor at the terminal to find out which editor you were stuck in for sure. You’ll get a line back that reads something like editor = vim, which would be the name of the editor that git defaults to. In the future, you can use the standard method to exit that editor if, after a normal git merge, you see the “Please enter a commit message to explain why this merge is necessary” line again.

You might also wish to edit the file, with whatever text editor you usually prefer, and change the editor to whatever it is you like. Scroll down to where it reads [core] and then change the line that reads “editor = vim” to read whichever one you’d like. For instance, you might want it to read “editor = nano” if you prefer to use the nano editor to code with.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.