AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1419613
Accepted
SoniEx2
SoniEx2
Asked: 2019-04-01 06:16:56 +0800 CST2019-04-01 06:16:56 +0800 CST 2019-04-01 06:16:56 +0800 CST

更改 git init 默认分支名称

  • 772

我试图弄清楚如何git init使用不同于master第一次提交的默认分支名称,但我找不到它的 git 配置或任何允许我这样做的东西(例如别名只适用于命令,而不是分支名称)。

有什么方法可以更改设置的默认第一个分支名称git init吗?

git
  • 7 7 个回答
  • 77356 Views

7 个回答

  • Voted
  1. T. Kiley
    2020-07-28T13:36:15+08:002020-07-28T13:36:15+08:00

    从 Git 2.28(2020 年 7 月 27 日发布)开始,您现在可以配置在初始化新存储库时创建的分支的名称:

    $ git config --global init.defaultBranch main
    

    设置此变量后,运行git init将生成一个存储库,其初始分支为main:

    $ git init
    Initialised empty Git repository in /home/thomas/test-git-repo/.git/
    $ git status
    On branch main
    
    No commits yet
    
    nothing to commit (create/copy files and use "git add" to track)
    

    发行说明:https ://lore.kernel.org/git/[email protected]/

    • 284
  2. Ikke
    2019-04-01T09:28:09+08:002019-04-01T09:28:09+08:00

    在 2.28 之前的 Git 版本中,HEAD硬编码为指向refs/heads/master.

    if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
    

    所以没有配置设置或选项可以传递给git init将其更改为其他内容。

    但是,有可能在以下帮助下更改HEAD指向的内容:git initgit symbolic-ref

    $ git init
    $ git symbolic-ref HEAD refs/heads/test
    

    这将更HEAD改为指向一个名为 的(尚不存在的)分支test。然后,当您创建第一个提交时,将调用该分支test而不是master.

    • 26
  3. Best Answer
    SoniEx2
    2020-06-11T07:51:23+08:002020-06-11T07:51:23+08:00

    更改默认 HEAD 的一种简单方法是在 git 模板目录中创建一个 HEAD。首先,将您的模板目录配置为~/Templates/git.git(或您喜欢的任何内容):

    $ git config --global init.templateDir '~/Templates/git.git'
    $ cp -r /usr/share/git-core/templates ~/Templates/git.git
    

    然后,HEAD在模板目录中创建文件:

    $ echo 'ref: refs/heads/default' > ~/Templates/git.git/HEAD
    

    你很高兴去!无论何时运行git init,您现在都会收到消息:

    $ git init
    Reinitialized existing Git repository in [...]
    

    出于某种原因,git 决定是否使用此消息基于HEAD文件在 中的存在.git,而不是依赖于是否.git必须创建。但是,它向您显示什么消息并不重要。从 git-init 手册页:

    git init在现有存储库中运行是安全的。它不会覆盖已经存在的东西。重新运行的主要原因git init是选择新添加的模板(或者将存储库移动到另一个地方,如果有的话--separate-git-dir)。

    也就是说,git init保证不会覆盖HEAD你放在模板中的,也不会用模板HEAD覆盖已有HEAD的。由于这是明确记录的,因此您可以依赖它。

    此外,它还说:

    模板目录下名称不以点开头的文件和目录将在$GIT_DIR创建后复制到模板目录中。

    这意味着您还可以依赖在创建后立即复制的模板.git,而不是稍后复制。

    (当然,这是我个人对手册的解读,完全有可能git开发者不同意。)

    • 17
  4. wsams
    2021-05-04T09:59:54+08:002021-05-04T09:59:54+08:00

    设置默认分支的一种方法是编辑~/.gitconfig文件。添加以下行:

    [init]
      defaultBranch = main
    

    现在当你运行时git init,main将是你的默认分支。这类似于git config --global init.defaultBranch main@t-kiley 提到的跑步。

    • 10
  5. Davey
    2019-04-12T06:31:57+08:002019-04-12T06:31:57+08:00

    如前所述,HEAD 被硬编码为指向 master。但是,您可以创建一个 shell 别名,以随心所欲git init。如果你使用 bash 作为你的 shell,把这样的东西放在你的 .bashrc 中:

    function git_init_fnc () {
      default_branch="main"
      if [[ $1 == "init" ]] && [[ $# -eq 1 ]];then
          git init
          if [[ ! -z $(git branch -a | grep $default_branch) ]]; then
              git checkout "$default_branch"
          else
              git checkout -b "$default_branch"
          fi
      else
          /usr/bin/git "$@"
      fi
    }
    
    alias "git"=git_init_fnc
    

    这将用git函数替换命令。此函数将使命令 git运行完全相同,除非您在调用git init时不带任何其他参数。当您调用git init它时,它将初始化存储库。接下来它将检查分支“daddy”是否已经存在。如果是,它将检出该分支,否则它将创建该分支并将您移至该分支。

    • 3
  6. Thomas Praxl
    2021-12-24T11:36:44+08:002021-12-24T11:36:44+08:00

    该线程的大部分答案都适用于某些 git 版本,但没有答案适用于我正在使用的所有版本(git 版本和2.7)。2.252.30

    但多亏了你们所有的回答,我才能够混合你们的共同想法,我终于找到了一种通过一种设置支持所有版本的方法:

    [编辑:原来,我的观察是错误的。由于我的特殊设置,它没有用。如果您对上述解决方案有疑问,
    这里有另一种方法(针对 git 版本2.7和测试)。]2.252.30

    init.defaultBranch结合包装函数(仅适用于不支持 init.defaultBranch 的 git 版本,它在会话开始时被查询一次)。

    包装函数在 init 之后直接向配置的默认分支发出 git checkout。

    .gitconfig

    …
       [init]
        # Usually only works for git >= 2.28.
        # Thus, these dotfiles provide a git wrapper that performs extra
        # action after git init. The git wrapper takes this setting
        # into account, so that it even works for git < 2.28.
        defaultBranch = "main"
    …
    

    bash 别名

    #!/usr/bin/env bash
    
    main() {
      wrap_git_if_necessary
    }
    
    # if the current git version does not support a custom
    # default branch (init.defaultBranch, e.g. main),
    # then we wrap it in a function that performs
    # extra action on git init.
    wrap_git_if_necessary() {
      if ! supports_default_branch &>/dev/null; then
        alias git=git_wrapper
      fi
    }
    
    supports_default_branch() {
      command git help --config | grep "init.defaultBranch"
    }
    
    # wrap git to make main the default branch on init.
    # Is required for git versions < 2.28, which do
    # not support init.defaultBranch natively.
    git_wrapper() {
      local defaultBranch
      if command git "$@"; then
        if [[ "$1" == "init" ]]; then
          defaultBranch=$(command git config init.defaultBranch || echo "main")
          command git checkout -b "${defaultBranch}"
        fi
      fi
    }
    
    main "$@"
    
    
    • 1
  7. guyr
    2022-02-14T02:16:58+08:002022-02-14T02:16:58+08:00

    我通过搜索找到了这个问题。我意识到我在回答一个老问题,但对于可能到达这里的其他人,GitHub 有一个页面,介绍如何为在那里创建的任何存储库配置主分支名称,以及更改主分支名称的说明:

    从 master 重命名默认分支

    • 0

相关问题

  • 混帐添加。命令不工作

  • 提交后从git恢复已删除的文件

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    Windows 10 服务称为 AarSvc_70f961。它是什么,我该如何禁用它?

    • 2 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Marko Smith

    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1056)

    • 4 个回答
  • Marko Smith

    我如何知道 Windows 安装在哪个驱动器上?

    • 6 个回答
  • Martin Hope
    Albin 支持结束后如何激活 WindowsXP? 2019-11-18 03:50:17 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    Kagaratsch Windows 10 删除大量小文件的速度非常慢。有什么办法可以加快速度吗? 2019-09-23 06:05:43 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    Inter Sys Ctrl+C 和 Ctrl+V 是如何工作的? 2019-05-15 02:51:21 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve