合并后的 Git 分支和提交历史记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7274938/
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
Git branches & commits history after merge
提问by Ansd
I'm working on a project (alone) and for every feature I develop I create a new branch, work on this feature, then merge it to master. So normally I never work on two different branches at one time and never touch master while working on a branch.
我正在处理一个项目(单独),对于我开发的每个功能,我创建一个新分支,处理此功能,然后将其合并到 master。所以通常我从不同时在两个不同的分支上工作,也从不在一个分支上工作时接触 master。
When I merge a branch I see that (using gitx
and gitk
) the history of master branch gets all the commits I've done to the merged branch. I mean if I have something like:
当我合并一个分支时,我看到(使用gitx
和gitk
) master 分支的历史记录获取了我对合并分支所做的所有提交。我的意思是如果我有类似的东西:
master a-b-c-d
\z-x-y--
|branch name
after merge I get:
合并后我得到:
a-b-c-d-z-x-y
|branch name
Yes, I see the merged branch name highlighted (using gitx
and gitk
), but what I was expecting is something showing exactly where commits are done (to which branch) like:
是的,我看到合并的分支名称突出显示(使用gitx
和gitk
),但我期望的是显示提交完成的确切位置(到哪个分支),例如:
master a-b-c-d--------M--
\-z-x-y-/
|branch name
So I'm expecting to see a commit "M" that represents the merge I've done to master, not to look like that all commits I've done to the new branch have been done to master.
所以我期待看到一个提交“M”,它代表我对 master 所做的合并,而不是看起来我对新分支所做的所有提交都已经完成了 master。
Is my expectation correct? Or this is normal git
behaviour?
我的预期正确吗?或者这是正常git
行为?
回答by Cameron Skinner
That is normal Git behaviour. You are doing what is called a "fast-forward" merge, because your branch is strictly ahead of the master
branch.
这是正常的 Git 行为。您正在执行所谓的“快进”合并,因为您的分支完全领先于master
分支。
If you really want to preserve branch history (although I'd recommend you don't bother) then you can use git merge --no-ff
to force it to create a merge commit even when it can do a fast-forward update.
如果您真的想保留分支历史记录(尽管我建议您不要打扰),那么您可以使用git merge --no-ff
强制它创建合并提交,即使它可以进行快进更新。
回答by VonC
You can found addition criticism to the -no-ff option in "Understanding the Git Workflow", mainly because it would break git blame
.
More at "fast forward when using pull
and no-ff
when pull
".
您可以在“了解 Git 工作流程”中找到对 -no-ff 选项的额外批评,主要是因为它会破坏git blame
.
更多信息参见“使用时快进pull
和no-ff
何时快进pull
”。
As explained in "Why does git use fast-forward merging by default?", unless you are talking about a really long-lived branch, a fast-forward mergeis preferable.
正如“为什么 git 默认使用快进合并?”中所述,除非您谈论的是真正长期存在的分支,否则最好使用快进合并。