How can I create a Git repository with the default branch name other than “master”?

后端 未结 4 1317
感情败类
感情败类 2020-12-22 16:35

In the Pro Git book, it says

“origin” is not special

Just like the branch name “master” does not have any special meaning in Git, neither does “or

4条回答
  •  青春惊慌失措
    2020-12-22 17:22

    You can, indirectly, configure git init to use a different default branch: the current branch is defined by HEAD, which is “just” a textfile telling Git which ref is the current one.

    Using init.templateDir, you can ask git init to use a different one:

    # ~/.config/git/config or ~/.gitconfig
    [init]
        templateDir = ~/.config/git/template/
    

    and in ~/.config/git/template/HEAD, put a single line (+ linebreak): ref: refs/heads/main (to default to branch main).

    The whole contents of templateDir are copied to the .git directory when creating a repository; the default (here /usr/share/git-core/templates) contains some sample hooks and other files, but you can use your new template directory to setup default hooks, for example.

    $ tree /usr/share/git-core/templates
    /usr/share/git-core/templates
    ├── branches
    ├── description
    ├── hooks
    │   ├── applypatch-msg.sample
    │   ├── commit-msg.sample
    │   ├── fsmonitor-watchman.sample
    │   ├── post-update.sample
    │   ├── pre-applypatch.sample
    │   ├── pre-commit.sample
    │   ├── prepare-commit-msg.sample
    │   ├── pre-push.sample
    │   ├── pre-rebase.sample
    │   ├── pre-receive.sample
    │   └── update.sample
    └── info
        └── exclude
    
    3 directories, 13 files
    

提交回复
热议问题