The error message “Please enter a commit message to explain why this merge is necessary” is one that you probably encounter while working as a DevOps engineer or on a git repository. I’ll go into the reasons for this mistake, how to repair it, and some solutions in this post.
Why git asked me “Please enter a commit message to explain why this merge is necessary”?
For a few specific reasons, git pull requests that we create a merge message. Let me begin with a quotation from Linus Torvalds:
The causes listed below could lead to a warning in the Git commit message:
- While importing updates from git onto your local development
- Your git client was recently updated.
- You recently changed your git configuration. Prior to this, you never had a local branch that was ahead of the remote.
How to Fix “Please enter a commit message to explain why this merge is necessary” warning.
Depending on what you want to do, choose one of these two options.
Solution-1 If you want to merge happen and then exit from your default editor
If you would want to merge but are receiving the notice “Please enter a commit message to explain why this merge is necessary,” you can remedy it by using the solutions listed below, which are depending on your editor.
For vi or vim editor users in Linux –
In Linux, the default editor is vi or vim, unless you have modified it using the git config command. Simply follow these instructions to merge according to your editor:
- Type “i” on the keyboard to enter your “Merge” remarks or message.
- Click the “Esc” key on your keyboard.
- Use the shortcut :wq to save and close your Vi or Vim editor.
Alternatively,
if you don’t want to give a Merge message, then you can simply press “Esc” on your keyboard and then shift + ZZ to save and exit from the editor quickly.
For nano or Pico users in macOS –
If you are using nano or pico editor in macOS or maybe in WSL (Windows Subsystem for Linux) then run the following command –
- Press CTRL + X
- Then CTRL + C
- Press “Y” to save your file.
If you want to change the default git editor e.g. from vi/vim to nano run the following command –
git config --global core.editor "nano"
Option #2 In the event that you would want to leave without merging and not wish to merge.
Thus, if you keep receiving the following git merge error:
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
and you choose to come clean rather than combine, the procedure can be a little challenging. Simply follow the instructions provided.
For vi/vim or nano editor users
- First suspend the vi/vim or nano editor by pressing ctrl + z on your keyboard
- Make a note of the Pid (Process ID) or process name. e.g. nano or vi.
- Abort the merge with a git merge --abort command
- Kill the background process with pkill -9 “process name“. For e.g.
pkill -9 nano
orpkill -9 vi
. You will get a message, that process got killed successfully.
Suggestion – Git merge vs Rebase to keep a clean history in Git
I have told you the solution on how to get rid of the “Please enter a commit message to explain why this merge is necessary” error. But probably I must tell you that when you can use Git Merge and when to use rebase while git pull.
Follow these golden rules while working with Git –
Rule 1 – Always rebase when pulling changes from origin/develop onto your local develop
Always use rebase option, while pulling changes from origin instead of just running the git pull command alone.
git pull --rebase
Why? because git pull and git pull –rebase are the same if you are pulling from the origin and you haven’t changed anything locally developed.
However, if you have made a few unpushed modifications to your local development. Before pushing, you should also pull any changes you missed from your origin to your local develop. Merge problems could arise from a routine git pull without the rebase option.
Rule 2: Merge back to develop after completing a feature branch merge
When you want to merge modifications back into development after making changes, use the following command.
git merge
Conclusion
Depending on your needs, I’m sure you can easily remove the “Please enter a commit message to explain why this merge is necessary” error if you carefully follow this thread.
If you continue to have problems, please let me know via your comments, and I will assist you.