If you are a DevOps engineer or you work on a git repository, Then "Please enter a commit message to explain why this merge is necessary" is the common error you get while working. In this post, I will explain, why this error occurs, how to fix this error and suggestions around it.
Why git asked me "Please enter a commit message to explain why this merge is necessary"?
There are some specific reasons, why git pull asks us to write a merge message. I would like to start with a quote from Linus Torvalds -
Below mentioned are the reasons, which may result in Git commit message warning -
- When you are pulling changes from git onto your local develop
- You have recently updated your git client
- Your git config was change recently
- You never had a local branch which was ahead of the remote before
How to Fix "Please enter a commit message to explain why this merge is necessary" warning.
Follow one of these two solutions, depending upon what are you trying to achieve?
Solution-1 If you want to merge happen and then exit from your default editor
In case you are getting the " Please enter a commit message to explain why this merge is necessary " message and you want to merge, then follow the below-mentioned solutions based on your editor to fix it.
For vi or vim editor users in Linux -
The default editor is vi or vim in Linux, if you have not changed it git config command. Follow these simple steps to merge based on your editor -
- Press "i" on your keyboard, type your "Merge" comments or message
- Press "Esc" button on your keyboard
- Save and exit by Typing :wq from your Vi or Vim editor
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"
Solution-2 If you don't want to merge happen and want to exit without merging.
So if you are getting the following git merge error again and again -
# 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 don't want to merge and want to come clean, then the process is a bit tricky. Just follow provided steps -
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 nanoor
pkill -9 vi. You will get a message, that process got killed successfully.
Please make a note, even if you press ESC and then :q or :q! to exit vi or vim editor without saving. Still, it results in the merge. The same applies to other editors like nano. If you press ctrl + x to exit, still branch will merge. So make sure you always suspend and abort the process to avoid merging as explained in the above steps.
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 if you are pulling from the origin and you have not made any changes to your local develop, then git pull and git pull --rebase are the same.
But if you have made a few changes to your local develop that have not been pushed yet. Also, you want to pull any changes you are missing from your origin to your local develop before you can push. A regular git pull without rebase option may result in merge issues.
Rule 2 - Merge when finishing a feature branch merge change back to develop
Use the following command, when you have made changes and want to merge changes back to develop
I am sure, if you will follow this post carefully, depending upon your requirements, you will easily get rid of the "Please enter a commit message to explain why this merge is necessary" error.
Let me know via your comments, in case you still face any issues, I will help you.