【问题标题】:How do I create a master branch in a bare Git repository?如何在裸 Git 存储库中创建主分支?
【发布时间】:2026-01-12 01:05:02
【问题描述】:
git init --bare test-repo.git
cd test-repo.git

(文件夹是用 git-ish 文件和文件夹创建的)

git status

致命:此操作必须在工作树中运行 (好的,所以我不能将 git status 与裸仓库一起使用;我猜是有道理的)

git branch

(什么都没有,看起来裸仓库不包含任何分支。我必须从克隆的仓库中添加它们吗?)

cd ..
mkdir test-clone
cd test-clone
git clone ../test-repo.git

(我收到有关克隆空存储库的警告)

cd test-repo

(提示改变以表明我在 master 分支上)

git branch

(没有显示结果 - 嗯?)

git branch master

致命:不是有效的对象名称:'master'

嗯。那么如何在我的裸仓库中创建 master 分支呢?

【问题讨论】:

    标签: git git-branch git-bare


    【解决方案1】:

    裸存储库几乎是您只能推送和获取的东西。你不能直接“在里面”做很多事情:你不能检查东西,创建引用(分支,标签),运行git status,等等。

    如果您想在裸 Git 存储库中创建新分支,可以将分支从克隆推送到裸存储库:

    # initialize your bare repo
    $ git init --bare test-repo.git
    
    # clone it and cd to the clone's root directory
    $ git clone test-repo.git/ test-clone
    Cloning into 'test-clone'...
    warning: You appear to have cloned an empty repository.
    done.
    $ cd test-clone
    
    # make an initial commit in the clone
    $ touch README.md
    $ git add . 
    $ git commit -m "add README"
    [master (root-commit) 65aab0e] add README
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 README.md
    
    # push to origin (i.e. your bare repo)
    $ git push origin master
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To /Users/jubobs/test-repo.git/
     * [new branch]      master -> master
    

    【讨论】:

    • 对我不起作用。 test-clone>git commit -m "add README" On branch master 初始提交 未跟踪的文件:README.md 没有添加到提交但未跟踪的文件存在
    • @LawrenceDeSouza 你很可能忘了git add .
    【解决方案2】:

    分支只是对提交的引用。在您向存储库提交任何内容之前,您没有任何分支。您也可以在非裸存储库中看到这一点。

    $ mkdir repo
    $ cd repo
    $ git init
    Initialized empty Git repository in /home/me/repo/.git/
    $ git branch
    $ touch foo
    $ git add foo
    $ git commit -m "new file"
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 foo
    $ git branch
    * master
    

    【讨论】:

    • "分支只是对提交的引用。"谢谢,这真的有助于解释事情!
    • 如果您已经有文件,请使用 git add -A * 在其中添加内容。
    【解决方案3】:

    您不需要使用第二个存储库 - 只要您使用 --work-tree 选项提供一个虚拟工作目录,您就可以在裸存储库上执行 git checkoutgit commit 之类的命令。

    准备一个虚拟目录:

    $ rm -rf /tmp/empty_directory
    $ mkdir  /tmp/empty_directory
    

    在没有父级的情况下创建 master 分支(即使在完全空的 repo 上也可以):

    $ cd your-bare-repository.git
    
    $ git checkout --work-tree=/tmp/empty_directory --orphan master
    Switched to a new branch 'master'                  <--- abort if "master" already exists
    

    创建一个提交(它可以是一条消息,无需添加任何文件,因为您需要的只是至少有一个提交):

    $ git commit -m "Initial commit" --allow-empty --work-tree=/tmp/empty_directory 
    
    $ git branch
    * master
    

    清理目录,还是空的。

    $ rmdir  /tmp/empty_directory
    

    在 git 1.9.1 上测试。 (特别是对于 OP,posh-git 只是标准 git 的 PowerShell 包装器。)

    【讨论】:

    • “孤儿”标志是我所需要的。谢谢!
    • 我喜欢这种方法(出于脚本原因)。我会检查这是否有效,我很快就会回来给你一个加分。 :)
    【解决方案4】:

    默认情况下不会列出任何分支,只有在放置一些文件后才会弹出。你不必担心太多。只需运行所有命令,例如创建文件夹结构、添加/删除文件、提交文件、将其推送到服务器或创建分支。它可以无缝运行,没有任何问题。

    https://git-scm.com/docs

    【讨论】: