有没有一种方法来设置主机Git存储库,这样从它的(本地)克隆进行的任何Git拉取默认使用——rebase ?通过在Stack Overflow上搜索,我了解了分支。Autosetuprebase,但它需要每个克隆单独配置。

我的项目流程是这样设置的,我们在将一个特征分支合并到它之前拉开发分支。这个pull几乎总是使用。rebase,我试着弄清楚这是否可以是默认值。


当前回答

如何

git config --global pull.rebase true

这将告诉git总是拉与rebase。

其他回答

答案是否定的。

没有一种方法可以设置远程存储库,使每个克隆它的人都改变了git的默认行为。

但是,您可以设置一个服务器端钩子来检查是否没有人推送合并提交(也许类似这样)。

还有一些您可能会感兴趣的配置选项。所有从远程存储库克隆的开发人员都必须自己手动设置。

1. 选择分支。<名称> .rebase

你可以配置一个本地分支总是使用——rebase,像这样,用分支名称替换<name>:

git config branch.<name>.rebase true

在master上运行后,.git/config中的master部分是这样的:

[branch "master"]
    remote = origin
    merge = refs/heads/master
    rebase = true

2. 选择branch.autosetuprebase

为每个Git分支运行之前的config命令可能会很麻烦,所以你可以配置Git为每个新分支自动设置:

git config branch.autosetuprebase always

(你也可以指定never, remote和local,详见man git-config。)

如果没有——global选项,配置将保存到.git/config,并且只影响当前存储库。使用——global,配置保存到~/。Gitconfig,并且每个未配置的存储库都会受到影响。

该选项不会影响已经存在的分支。

3.选择pull.rebase

git config pull.rebase true

(你也可以给它——global选项。)

如果这个选项为真,运行git pull等同于git pull——rebase,除非branch.<name>。Rebase已设置为false。

如何

git config --global pull.rebase true

这将告诉git总是拉与rebase。

缺省的拉行为现在有3个不同级别的配置。从最一般的到最细的,它们是:

1. pull.rebase

将此设置为true意味着git pull总是等价于git pull——rebase(除非branch.<branchname>。Rebase显式设置为false)。这也可以为每个存储库或全局设置。

2. branch.autosetuprebase

将此设置为总是意味着每当创建跟踪分支时,将为其创建如下所示的配置条目。对于更细粒度的控制,也可以将其设置为never、local或remote,并且可以为每个存储库设置或全局设置。详见git config——help。

3.分支。< branchname > .rebase

将此设置为true意味着该特定分支将始终通过rebase从其上游进行拉取,除非显式地使用git pull——no-rebase。

结论

因此,虽然您不能更改存储库的所有未来克隆的默认行为,但您可以通过git config -global pull更改当前用户(现有和未来)存储库的默认行为。变基真的。

这使得——rebase选项成为对给定分支执行git pull时的默认选项。

@Flimm,我需要添加真,使你的第一个选择工作。

所以正确的语法是:

git config branch.<branch>.rebase true

在develop分支上运行此命令:

git config branch.develop.rebase true

现在.git/config中的开发部分看起来像这样:

[branch "develop"]
        remote = origin
        merge = refs/heads/develop
        rebase = true

目前没有办法为存储库设置默认策略。

如果你想自己使用它,并且至少使用git 1.7.9,你可以全局设置pull。调整配置如下:

git config --global pull.rebase true

但是你必须在每台机器上做。一个选项是使用该选项配置默认用户主页模板/框架。不过,用户可能会改变这个选项。

如果您不想要合并,您可以定义一个服务器端钩子来拒绝带有合并的推送。

供您参考,他是pull.rebase的源文档:

When true, rebase branches on top of the fetched branch, instead of merging the default branch from the default remote when "git pull" is run. See "branch..rebase" for setting this on a per-branch basis. When merges, pass the --rebase-merges option to git rebase so that the local merge commits are included in the rebase (see git-rebase for details). When preserve, also pass --preserve-merges along to git rebase so that locally committed merge commits will not be flattened by running git pull. When the value is interactive, the rebase is run in interactive mode. NOTE: this is a possibly dangerous operation; do not use it unless you understand the implications (see git-rebase for details).