如何让 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
How to make Git pull use rebase by default for all my repositories?
提问by Masked Man
Is there a way to setup the host Git repository such that any git pull
done from its (local) clones uses --rebase
by 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 pull
the develop
branch before merge
ing a feature branch to it. This pull
nearly 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 true
means that git pull
is always equivalent to git pull --rebase
(unless branch.<branchname>.rebase
is 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 always
means 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
, local
or remote
and can be set per repository or globally. See git config --help
for further details.
将此设置为always
意味着无论何时创建跟踪分支,都会为其创建如下所示的配置条目。对于更细粒度的控制,这也可以设置为never
,local
或者remote
并且可以为每个存储库或全局设置。有关git config --help
更多详细信息,请参阅。
3. branch.<branchname>.rebase
3. branch.<branchname>.rebase
Setting this to true
means that that particular branch will always pull from its upstream via rebasing, unless git pull --no-rebase
is 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 pull
changed.
没有办法设置远程存储库,以便克隆它的每个人都具有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 master
section in .git/config
looked 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-config
for details.)
(您还可以指定never
、remote
和local
,man git-config
有关详细信息,请参阅。)
Without the --global
option, 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 --global
option.)
(你也可以给它这个--global
选项。)
If this option is true, running git pull
is equivalent to git pull --rebase
, unless branch.<name>.rebase
has been set to false
.
如果此选项为 true,则运行git pull
等效于git pull --rebase
,除非branch.<name>.rebase
已设置为false
。
回答by Daishi
This makes the --rebase
option the default when issuing a git pull
on a given branch.
这使该--rebase
选项成为git pull
在给定分支上发出 a 时的默认选项。
@Flimm, I needed to add true
to make your first option work.
@Flimm,我需要添加true
才能使您的第一个选项起作用。
So the correct syntax is:
所以正确的语法是:
git config branch.<branch>.rebase true
To run this command on the develop
branch:
要在develop
分支上运行此命令:
git config branch.develop.rebase true
And now the develop
section in .git/config
looks 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.rebase
configuration 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)。