为什么 git 要求输入提交消息以解释为什么需要进行此合并

Why git asks to enter a commit message to explain why this merge is necessary

我在我的本地分支上有 1 个提交,然后为了将更改从远程分支带到我的本地,我在我的本地分支上做了一个 git pull 并且令我惊讶的是 git 说了这个。

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

我知道我在 vi 编辑器中。

我的问题是为什么 git要我输入消息。我以前从来没有遇到过。

我的 git 版本是:version 1.9.5-preview20141217

我提到了 this 问题,但我仍然觉得它很难理解。 谢谢。

来自 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

关于您的问题:为什么

  1. 为什么 Git 创建 MERGE 提交:这是 git pull 的默认行为。网上有很多关于这种行为的解释,this 解释得很好。
  2. 为什么 Git 现在要求提交消息:我想到了三个可能的选项:
    • 您更新了 git 客户端
    • 你以前从来没有一个本地分支在远程之前
    • 您的 git 配置最近已更改

如何避免这种情况:

由于您的本地存储库提前 1 次提交,git 尝试将您的远程存储库合并到本地存储库。这可以通过合并来处理,但在你的情况下,也许你正在寻找变基,即将你的提交添加到顶部。您可以使用

git rebasegit pull --rebase

如果这确实是您正在寻找的行为,您可以设置 git 配置,使变基成为 git pull

的默认选项

全局设置:

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

或者您可以按分支设置:

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