Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

People also ask

What should git pull do by default?

In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD . More precisely, git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch.

What is git config pull rebase?

Pull with RebaseThis configuration switches that behavior to the rebasing strategy. With rebasing, new commits will be created for the changes on your local branch that start after the changes on the remote branch.

What is git config pull ff only?

git pull --ff-only corresponds to git fetch git merge --ff-only origin/master. --ff-only applies the remote changes only if they can be fast-forwarded. From the man: Refuse to merge and exit with a non-zero status unless the current HEAD is already up-to-date or the merge can be resolved as a fast-forward.

Is git pull -- rebase the same as git rebase?

Regarding your question: git rebase rebases the branch you want. git pull --rebase performs a fetch + rebase in the branches you pull.


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.


How about

git config --global pull.rebase true

This will tell git to always pull with rebase.


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.


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

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