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

Dav*_*vid 38 git git-bare git-branch posh-git

(全部在Windows 8上以poshgit完成):

git init --bare test-repo.git
cd test-repo.git
Run Code Online (Sandbox Code Playgroud)

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

git status
Run Code Online (Sandbox Code Playgroud)

致命:这个操作必须在工作树中运行 (好吧,所以我不能使用git状态和一个简单的仓库;我猜是有道理的)

git branch
Run Code Online (Sandbox Code Playgroud)

(没什么,似乎裸仓库不包含任何分支.我是否必须从克隆的仓库中添加它们?)

cd ..
mkdir test-clone
cd test-clone
git clone ../test-repo.git
Run Code Online (Sandbox Code Playgroud)

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

cd test-repo
Run Code Online (Sandbox Code Playgroud)

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

git branch
Run Code Online (Sandbox Code Playgroud)

(显示没有结果 - 是吗?)

git branch master
Run Code Online (Sandbox Code Playgroud)

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

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

jub*_*0bs 56

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


che*_*ner 12

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

$ 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
Run Code Online (Sandbox Code Playgroud)

  • "分支只是对提交的引用." 谢谢,这真的有助于解释事情! (3认同)

kub*_*zyk 7

无需第二个存储库。我们可以通过提供一个虚拟目录--work-tree选项,像命令checkoutcommit开始即使在纯仓库工作。准备一个虚拟目录:

$ rm -rf /tmp/empty_directory
$ mkdir  /tmp/empty_directory
Run Code Online (Sandbox Code Playgroud)

然后你去了:

$ 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
Run Code Online (Sandbox Code Playgroud)

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

  • “孤儿”标志正是我所需要的。谢谢! (3认同)