如何在裸Git存储库中创建主分支?

时间:2014-12-24 11:11:10

标签: git git-branch git-bare posh-git

(所有在Windows 8上以poshgit完成):

git init --bare test-repo.git
cd test-repo.git

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

git status

致命:此操作必须在工作树中运行 (好吧,所以我不能使用git状态和一个简单的回购;我猜是有意义的)

git branch

(没什么,看起来裸露的回购并不包含任何分支。我是否必须从克隆的回购中添加它们?)

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

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

cd test-repo

(提示符更改以指示我在主分支上)

git branch

(显示没有结果 - 嗯?)

git branch master

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

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

4 个答案:

答案 0 :(得分:48)

裸存储库几乎是你只需要推送和获取的东西。你不能直接做很多事情"在其中#34;你不能检查东西,创建引用(分支,标签),运行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

答案 1 :(得分:10)

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

$ 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

答案 2 :(得分:5)

无需第二个存储库。我们可以使用--work-tree选项提供虚拟目录,checkoutcommit等命令甚至可以在裸存储库上运行。准备一个虚拟目录:

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

然后你去了:

$ cd test-repo.git             # your fresh bare repository

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

$ git --work-tree=/tmp/empty_directory   commit --allow-empty -m "empty repo" 

$ git branch
* master

$ rmdir  /tmp/empty_directory

在vanilla git 1.9.1上测试过。我没有验证posh-git是否支持--allow-empty进行提交而不进行任何文件更改(仅消息提交)。

答案 3 :(得分:0)

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

https://git-scm.com/docs

相关问题