如何让 Git pull 默认为我的所有存储库使用 rebase?

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

How to make Git pull use rebase by default for all my repositories?

git

提问by Masked Man

Is there a way to setup the host Git repository such that any git pulldone from its (local) clones uses --rebaseby default? By searching on Stack Overflow, I learned about branch.autosetuprebase, but it needs to be configured per clone individually.

有没有办法设置主机 Git 存储库,以便默认情况下git pull使用从其(本地)克隆完成的任何操作--rebase?通过在 Stack Overflow 上搜索,我了解了branch.autosetuprebase,但需要为每个克隆单独配置。

My project flow is set up such that we pullthe developbranch before mergeing a feature branch to it. This pullnearly always uses --rebase, so I am trying to figure out if this can be the default.

我的项目流程是这样设置的,这样我们就可以pull在向其添加功能分支develop之前merge创建分支。这pull几乎总是使用--rebase,所以我想弄清楚这是否可以作为默认值。

回答by Parker Coates

There are now 3 different levels of configuration for default pull behaviour. From most general to most fine grained they are:

现在有 3 个不同级别的默认拉取行为配置。从最一般到最细粒度,它们是:

1. pull.rebase

1. pull.rebase

Setting this to truemeans that git pullis always equivalent to git pull --rebase(unless branch.<branchname>.rebaseis explicitly set to false). This can also be set per repository or globally.

将此设置为true意味着git pull始终等效于git pull --rebase(除非branch.<branchname>.rebase明确设置为false)。这也可以为每个存储库或全局设置。

2. branch.autosetuprebase

2. branch.autosetuprebase

Setting this to alwaysmeans that whenever a tracking branch is created, a configuration entry like the one below will be created for it. For finer grained control, this can also be set to never, localor remoteand can be set per repository or globally. See git config --helpfor further details.

将此设置为always意味着无论何时创建跟踪分支,都会为其创建如下所示的配置条目。对于更细粒度的控制,这也可以设置为neverlocal或者remote并且可以为每个存储库或全局设置。有关git config --help更多详细信息,请参阅。

3. branch.<branchname>.rebase

3. branch.<branchname>.rebase

Setting this to truemeans that that particular branch will always pull from its upstream via rebasing, unless git pull --no-rebaseis used explicitly.

将此设置为true意味着该特定分支将始终通过变基从其上游拉取,除非git pull --no-rebase明确使用。

Conclusion

结论

So while you can't change the default behaviour for all future clones of a repository, you can change the default for all of the current user's (existing and future) repositories via git config --global pull.rebase true.

因此,虽然您无法更改存储库所有未来克隆的默认行为,但您可以通过git config --global pull.rebase true.

回答by mackuntu

How about

怎么样

git config --global pull.rebase true

This will tell git to always pull with rebase.

这将告诉 git 始终使用 rebase 拉取。

回答by Flimm

The answer is no.

答案是不。

There isn't a way to set up a remote repository so that everyone who clones it has the default behaviour of git pullchanged.

没有办法设置远程存储库,以便克隆它的每个人都具有git pull更改的默认行为。

You can, however, set up a server-side hook that checks that no one pushes merge commits (something like this, perhaps).

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

There are also some configuration options that you may be interested in. All the developers who clone from the remote repository will have to set it themselves manually.

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

1. Option branch.<name>.rebase

1. 选项 branch.<name>.rebase

You can configure a local branch to always use --rebase, like this, replacing <name>with a branch name:

您可以将本地分支配置为始终使用--rebase,如下所示,替换<name>为分支名称:

git config branch.<name>.rebase true

After running this on master, the mastersection in .git/configlooked like this:

在 上运行后master, 中的master部分.git/config如下所示:

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

2. Option branch.autosetuprebase

2. 选项 branch.autosetuprebase

Running that previous config command for every Git branch can be a hassle, so you can configure Git to automatically set it up for every new branch:

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

git config branch.autosetuprebase always

(You can also specify never, remote, and local, see man git-configfor details.)

(您还可以指定neverremotelocalman git-config有关详细信息,请参阅。)

Without the --globaloption, the configuration is saved to .git/config, and only the current repository is affected. With --global, the configuration is saved to ~/.gitconfig, and every unconfigured repository is affected.

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

This option does not affect already existing branches.

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

3. Option pull.rebase

3. 选项 pull.rebase

git config --bool pull.rebase true

(You can also give it the --globaloption.)

(你也可以给它这个--global选项。)

If this option is true, running git pullis equivalent to git pull --rebase, unless branch.<name>.rebasehas been set to false.

如果此选项为 true,则运行git pull等效于git pull --rebase,除非branch.<name>.rebase已设置为false

回答by Daishi

This makes the --rebaseoption the default when issuing a git pullon a given branch.

这使该--rebase选项成为git pull在给定分支上发出 a 时的默认选项。

@Flimm, I needed to add trueto make your first option work.

@Flimm,我需要添加true才能使您的第一个选项起作用。

So the correct syntax is:

所以正确的语法是:

git config branch.<branch>.rebase true

To run this command on the developbranch:

要在develop分支上运行此命令:

git config branch.develop.rebase true

And now the developsection in .git/configlooks like this:

现在 中的develop部分.git/config如下所示:

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

回答by David

Currently there is no way to set the default policy for a repository.

目前无法为存储库设置默认策略。

If you want it for yourself and you use at least git 1.7.9, you can globally set the pull.rebaseconfiguration as follow:

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

git config --global pull.rebase true

But you'll have to do on each machine. One option could be to configure the default user home template/skeleton with that option. Users might, however, change that option.

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

If you don't want merges, you could define a server-side hook to reject pushes with merges.

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

For your reference, his is the source documentationfor pull.rebase:

供您参考,他是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-rebasefor 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-rebasefor details).

如果为 true,则在获取的分支之上重新设置分支,而不是在运行“git pull”时从默认远程合并默认分支。请参阅“branch..rebase”以在每个分支的基础上进行设置。

合并时,将 --rebase-merges 选项传递给 git rebase,以便本地合并提交包含在 rebase 中( 有关详细信息,请参阅git-rebase)。

保留时,还将 --preserve-merges 传递给 git rebase,以便本地提交的合并提交不会通过运行 git pull 被扁平化。

当值为交互时,rebase 以交互模式运行。

注意:这是一个可能的危险操作;除非您了解其含义,否则请勿使用它(有关详细信息,请参阅git-rebase)。