Why git asks to enter a commit message to explain

2019-07-13 05:21发布

问题:

I had 1 commit on my local branch, then to take the changes from remote branch into my local, I did a git pull on my local branch and to my surprise git said this.

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

I understand that I'm in vi editor.

My question is why git asked me to enter the message. I never faced it before.

My git version is: version 1.9.5-preview20141217

I referred this question but I'm still finding it hard to understand. Thanks.

回答1:

From Git Documentation:

Incorporates changes from a remote repository into the current branch. In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD

As to your question about: WHY

  1. Why is Git creating a MERGE commit: That is the default behavior of git pull. There are lots of explanation of this behavior on the internet, this does a good job of explaining it.
  2. Why is Git asking for a commit message now: Three possible options come to my mind:
    • You updated your git client
    • You never had a local branch which was ahead of the remote before
    • Your git config was changed recently

How to avoid this:

Since your local repository is 1 commit ahead, git tries to merge your remote to your local repo. This can be handled via merge, but in your case, perhaps you are looking for rebase, i.e. add your commit to the top. You can do this with

git rebase or git pull --rebase

If this is indeed the behavior you are looking for, you can setup your git config to make rebase a default option for your git pull

Set it up globally with:

git config branch.autosetuprebase always # Force all new branches to automatically use rebase

Or you can set it up per branch:

git config branch.*branch-name*.rebase true # Force existing branches to use rebase.


标签: git git-pull