我可以在 git 中默认关闭快进吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2500296/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:06:14  来源:igfitidea点击:

Can I make fast forwarding be off by default in git?

gitmergebranchrebasefast-forward

提问by Jason Baker

I can't really ever think of a time when I would use git mergerather than git rebaseand notwant to have a commit show up. Is there any way to configure git to have fast forwarding off by default? The fact that there's an --ffoption would seem to imply that there's a way, but I can't seem to find it in the documentation.

我真的不能没有想过的时候,我会用git merge,而不是git rebase希望有一个提交露面。有什么方法可以将 git 配置为默认关闭快速转发?有一个--ff选项的事实似乎意味着有一种方法,但我似乎无法在文档中找到它。

回答by Eric Platon

It seems there is still a pending question in the thread: How to do it globally (i.e. for all branches) ? For the records, we can use the following:

线程中似乎还有一个悬而未决的问题:如何全局执行(即针对所有分支)?对于记录,我们可以使用以下内容:

git config --add merge.ff false

...to make it apply to all branches in the current repository. To make it apply to all branches in all repositorieswhere someone has notrun it without the --globaloption (local settings override global) run this:

...使其适用于当前存储库中的所有分支。要使其适用于所有存储库中的所有分支,如果有人没有在没有--global选项的情况下运行它(本地设置覆盖全局),请运行以下命令:

git config --global --add merge.ff false

From the documentation:

文档

merge.ff
By default, git does not create an extra merge commit when merging a commit that is a descendant of the current commit. Instead, the tip of the current branch is fast-forwarded. When set to false, this variable tells git to create an extra merge commit in such a case (equivalent to giving the --no-ffoption from the command line). When set to only, only such fast-forward merges are allowed (equivalent to giving the --ff-onlyoption from the command line).

merge.ff
默认情况下,git 在合并作为当前提交的后代的提交时不会创建额外的合并提交。相反,当前分支的尖端是快进的。当设置为 false 时,这个变量告诉 git 在这种情况下创建一个额外的合并提交(相当于--no-ff从命令行提供选项)。当设置为 only 时,只允许这样的快进合并(相当于--ff-only从命令行提供选项)。

回答by conny

Yes, there is --no-ff. You can configure merge options per branch, e.g.

是的,有--no-ff。您可以为每个分支配置合并选项,例如

git config branch.master.mergeoptions  "--no-ff"

adds the following to your $(REPO)/.git/configfile:

将以下内容添加到您的$(REPO)/.git/config文件中:

[branch "master"]
    mergeoptions = --no-ff


Footnote: speaking of my experience, I eventually found switching fast-forward to off was mostly helpful for git newcomers - however once the feel for workflows and concepts start to sink in you definitely want to avoid blurring your log graph with tons of pointless 'merged remote ..blarf' type commits.

脚注:说到我的经验,我最终发现将快进切换到关闭对 git 新手最有帮助 - 然而,一旦对工作流程和概念的感觉开始深入,你肯定希望避免用大量无意义的“合并”模糊你的日志图远程 ..blarf' 类型提交。

回答by bastian

Reading the thread of answers I ended up using the following two options

阅读答案线程我最终使用了以下两个选项

git config --global pull.ff only # Disallows non ff merges on pull. Overrides merge.ff when pulling
git config --global merge.ff false # even create extra merge commit when fast forward merge would be possible

Only loosely related I've also found this setting avoids trouble during pull

只是松散相关我还发现此设置可避免拉动过程中的麻烦

git config --global pull.rebase true # set up pull to rebase instead of merge