58
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'

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

4

4 回答 4

78

裸存储库几乎是您只能推送和获取的东西。你不能直接“在里面”做很多事情:你不能检查东西、创建引用(分支、标签)、运行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
于 2014-12-24T11:41:21.937 回答
24

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

$ 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
于 2014-12-24T16:20:30.147 回答
14

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

准备一个虚拟目录:

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

创建master没有父分支的分支(即使在完全空的仓库中也可以使用):

$ 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 包装器。)

于 2017-09-13T08:22:26.453 回答
-2

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

https://git-scm.com/docs

于 2018-06-01T13:06:04.690 回答