为什么 Git 创建一个没有文件更改的合并提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10157702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why did Git create a merge commit with no file changes?
提问by Alexis King
I am collaboratively working on a project with someone, so we decided to use git. Unfortunately, we frequently code in locations with no internet, so we end up with something like this:
我正在与某人合作开展一个项目,因此我们决定使用 git。不幸的是,我们经常在没有互联网的地方编码,所以我们最终会得到这样的结果:
origin/master: A---B---C
\
mylocalmaster: D---E---F
\
hismaster: G---H---I
Now, say he pushes his commits and gets this:
现在,假设他推送了他的提交并得到了这个:
origin/master: A---B---C---G---H---I
\
master (local): D---E---F
All I want to do is push my commits to get this in both my local repo and the online one:
我想要做的就是推动我的承诺在我的本地回购和在线回购中获得这个:
A---B---C---D---E---F---G---H---I
It seems to work when I do git push
, but the trouble arises when I do git fetch
and then git merge
. All I'm trying to do is get hiscommits into my local repo, but I end up with a merge commit saying something like Merge remote-tracking branch 'origin/master'
as its message.
这似乎工作时,我做的git push
,但是当我做的麻烦出现git fetch
,然后git merge
。我想要做的就是将他的提交放入我的本地存储库,但我最终得到了一个合并提交Merge remote-tracking branch 'origin/master'
,它说的是它的消息。
I don't want to have this pointless commit, since there is no conflicting code in our commits. We are working on completely different files, so there's no reason to have this commit. How can I prevent git from creating this merge commit?
我不想进行这种毫无意义的提交,因为我们的提交中没有冲突的代码。我们正在处理完全不同的文件,因此没有理由进行此提交。如何防止 git 创建此合并提交?
回答by Rafa? Rawicki
You can omit creating merge commits, by using rebaseinstead of merge.
您可以通过使用rebase而不是merge来省略创建合并提交。
As @Dougal said, if you do git fetch
, you can execute git rebase
afterwards to change the base of your changes to the fetched HEAD
.
正如@Dougal 所说,如果您这样做git fetch
,您可以在git rebase
之后执行以将更改的基础更改为 fetched HEAD
。
Usually you create those unwanted merge commits, by pulling from remote repository.
In that case you can add --rebase
option:
通常,您通过从远程存储库中提取来创建那些不需要的合并提交。在这种情况下,您可以添加--rebase
选项:
git pull --rebase
or add the proper option to Git config file (locally):
或将适当的选项添加到 Git 配置文件(本地):
git config branch.<branch-name-here>.rebase true
or for all the new repositories and branches:
或者对于所有新的存储库和分支:
git config branch.autosetuprebase always --global
However, rebasing creates cleaner, more linear history it is good to create merge commits, where there are massive changes in both branches(use git merge
to do so).
然而,变基创建更清晰、更线性的历史记录,创建合并提交是很好的,其中两个分支都有大量更改(用于git merge
这样做)。
回答by Dougal
Use git rebase
(after a git fetch
) to make your commits apply against his rather than against the previous master. That is, to go to ABCGHIDEF
in your example. (You can't do ABCDEFGHI
without having to do a push -f
, because ABCGHI
is already in origin/master
and you'd have to override that.)
使用git rebase
(在 a 之后git fetch
)使您的提交适用于他的而不是以前的主人。也就是说,ABCGHIDEF
在你的例子中去。(你不能不做ABCDEFGHI
a push -f
,因为ABCGHI
已经在里面origin/master
,你必须覆盖它。)