415

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.

8
  • 9
    Why do you want that? I think it's more reasonable to teach the users to actively think about which case will be more appropriate (based on the magnitude of changes they made or expect from upstream)' Commented Dec 12, 2012 at 18:35
  • 4
    @JonasWielicki Yes, I agree. It is just that some of my team members are new to Git, and I would like to know if there's a way to enforce it to avoid problems during the initial phase (until they have learned it). The team also works remotely in a different timezone, which means they would be stuck for several hours if something goes wrong. Just curious to know if this is possible.
    – Masked Man
    Commented Dec 12, 2012 at 18:47
  • 3
    I think especially for initial setups, it's better to go for merge. Rebase makes much more weird things if your code really diverges. You have to solve the same conflicts over and over again until you push. So if a team member wants to work on some code, always uses rebase and doesn't push until he's done (which newcomers may do, instead of branching themselves), they'll be faced with the same conflicts they have solved X times already. Commented Dec 12, 2012 at 18:52
  • 4
    @JonasWielicki The team members do make a new branch for each new feature they work on (and this, they have already understood quite well). The need for rebase comes because other developers have committed to the "remote" develop branch by the time he is ready to push his changes. Hence, I would like him to do a pull rebase from remote before pushing his changes. The project itself is quite mature, only the team is new. :) So it is a "initial setup" only in terms of people. What would be your advice for this scenario?
    – Masked Man
    Commented Dec 12, 2012 at 19:00
  • 11
    Replying to your first comment, in a majority of cases (almost all), rebase is the right choice, since it takes a lot of time to thoroughly test a new feature, etc. By the time that is done, there would most certainly be plenty of commits from other developers.
    – Masked Man
    Commented Dec 12, 2012 at 19:04

6 Answers 6

495

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.

2
  • 9
    Thanks for your response. I was exploring if I could have a setting so that anyone who clones the repository has it enabled by default. The above setting would be stored in ~/.gitconfig, which means each developer who clones the host repository would need to run the command. Not complaining about your solution. It is a good one, I just want to confirm I understood your point correctly.
    – Masked Man
    Commented Dec 20, 2012 at 17:01
  • 2
    Thanks for the answer. It does indeed look like this is as close as one can get.
    – Masked Man
    Commented Jan 3, 2013 at 8:59
372

How about

git config --global pull.rebase true

This will tell git to always pull with rebase.

6
  • 5
    Thanks, this works great for existing tracking branches.
    – Fls'Zen
    Commented May 30, 2014 at 17:39
  • 1
    this doesn't work for me: git config --global pull.rebase false
    – Nathan G
    Commented Nov 16, 2020 at 11:38
  • Great tip, now I can't pull anymore: fatal: Cannot rebase onto multiple branches.
    – leonheess
    Commented Apr 19, 2023 at 10:56
  • @leonheess git pull --merge. You're just changing the default; the merge option is still available.
    – flyx
    Commented Apr 21, 2023 at 7:15
  • @flyx but I want to rebase pull?
    – leonheess
    Commented Apr 21, 2023 at 8:32
83

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.

2
  • 5
    The only answer that actually answers the question.
    – kjbartel
    Commented Jun 23, 2021 at 0:38
  • 2
    Tip: enable the autoStash option for Git so that you can run "git pull --rebase" even with a dirty working tree: cscheng.info/2017/01/26/…
    – Flimm
    Commented Nov 1, 2023 at 15:46
8

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

5

If you are using .gitconfig and want to use merge instead of rebase when pulling, you can insert this snippet into your .gitconfig:

[pull]
  rebase = false

This will remove the annoying warning on each new project you set up.

1
  • 2
    OP is precisely asking for the opposite of what you're describing.
    – adamency
    Commented Aug 29, 2023 at 14:05
4

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
1
  • Thanks, I've edited my answer, in the future, feel free to edit the answer yourself.
    – Flimm
    Commented May 12, 2015 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.