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

Git

Git Problem Overview


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.

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

Git Solutions


Solution 1 - Git

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

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.

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.

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.

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.

Solution 2 - Git

How about

git config --global pull.rebase true

This will tell git to always pull with rebase.

Solution 3 - Git

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.

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

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

git config branch.<name>.rebase true

After running this on master, the master section in .git/config looked like this:

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

2. Option 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 branch.autosetuprebase always

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

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.

This option does not affect already existing branches.

3. Option pull.rebase

git config pull.rebase true

(You can also give it the --global option.)

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

Solution 4 - Git

This makes the --rebase option the default when issuing a git pull on a given branch.

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

So the correct syntax is:

git config branch.<branch>.rebase true

To run this command on the develop branch:

git config branch.develop.rebase true

And now the develop section in .git/config looks like this:

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

Solution 5 - Git

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 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 documentation for 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).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMasked ManView Question on Stackoverflow
Solution 1 - GitParker CoatesView Answer on Stackoverflow
Solution 2 - GitmackuntuView Answer on Stackoverflow
Solution 3 - GitFlimmView Answer on Stackoverflow
Solution 4 - GitDaishiView Answer on Stackoverflow
Solution 5 - GitDavidView Answer on Stackoverflow