Git pull 让我写合并消息

Git pull asks me to write merge message

我从我的分支:

git checkout mybranchSample
git fetch
git pull origin master

然后,Git 给我以下信息:

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

输入提交消息后,它会将 master 合并到我的文件中。即使我没有处理 master 中的某些文件,当我键入 git status.

时,它也会以绿色显示文件列表

这个问题没有发生在我​​的同事身上,但只发生在我身上。 这背后的原因是什么?

git pull 基本上是一次执行两个操作:git fetch 后跟 git merge(除非您使用 git pull --rebase,在这种情况下您可以猜到会发生什么)。

您看到此消息的原因是 Git 无法执行 fast-forward 合并,就像大多数时候一样。这样做的原因通常是因为您已经 git commit 在本地连接到您要拉取的分支,现在您需要将远程更改与本地更改合并。

还值得注意的是 Git pre-populated 合并消息是为您准备的,因此您实际上不需要输入任何内容。只需保存并退出,合并就应该完成了。 (当然,除非存在合并冲突)。

如果您尝试将 master 合并到 mybranchSample 分支,那么这是完全正常的。

Git 将 master 合并到 mybranchSample(当您从 mybranchSample 分支执行 git pull origin master 时)并且 commits 合并会为您更改(除非有任何冲突)并给你 pre-populated 信息。您可以保存并退出。

BUT 如果您只是想从远程 master 分支获取最新代码以与本地 master 分支合并,那么您应该检查出到 master,然后从 master.

拉出

来自你的声明

it merges master files into my files. And even though I havent worked on some files from master, it shows the files list in Green when I type git status.

我认为你正在尝试做第二个。

所以,试试:

git checkout master
git pull origin master

Linus Torvalds 解释得最好:

Spreading the word about an upcoming 'git' UI change, since I'm largely to blame.

This change hopefully makes people write merge messages to explain their merges, and maybe even decide not to merge at all when it's not necessary.

I've been using that git feature for the last few weeks now, and it has resulted in my merges from submaintainers having various notes in them (well, at least if the submainter gave me any). So I'm trying to lead by example.

But if you don't like explaining your merges, this might be annoying. Of course, if you don't explain your merges, you are annoying, so it all evens out in the end. "Karmic balance", so to say.

来源:https://plus.google.com/+LinusTorvalds/posts/SrePhcj6XJe

这只发生在您身上的原因不是您的配置不同,而是您要合并的分支不同。

diff 中的文件没有更改的原因是在合并之后,新提交必须是父提交:您上次执行的提交和 master 上的最新提交。新提交包含 all changes/files。因此,如果您将 newyour last commit 区分开来,您将看到所有 changed/added 的文件都是 changed/added master 而你的分支与 master 分支不同步。

我认为您的分支有问题...我可以想象您面临的 2 种可能情况:

1) 你有一个远程分支,mybranchSample,你想在它上面工作——你现在与 master 分支没有任何关系(也就是说,你不想将你的分支合并到 master 中,反之亦然)。你在问题中说

When I take pull from my branch after fetch like ...

但你所做的是 git pull origin master,它来自 master。相反,您应该能够使用 git 版本 >= 1.6.6,使用

检查您的远程分支
git fetch
git checkout mybranchSample

2) 您尝试在 mybranchSample 上工作,但想从那里的 master 分支获得最新代码。正如 Anand S 所说,合并可能是必要的。

尽量给大家一个简单的回答,不对的地方还请大家指正:

当您 git pull 在分支上提交后,这似乎会发生。您的本地更改在远程不存在,因此它提交以同步所有内容。