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

前端 未结 5 1596
礼貌的吻别
礼貌的吻别 2020-11-28 17:19

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

5条回答
  •  感情败类
    2020-11-28 18:23

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

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

    git config branch..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..rebase has been set to false.

提交回复
热议问题