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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27635955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:46:50  来源:igfitidea点击:

How do I create a master branch in a bare Git repository?

gitgit-branchgit-bareposh-git

提问by David

(All done in poshgit on Windows 8):

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

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

(Folder is created with git-ish files and folders inside)

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

git status

fatal: This operation must be run in a work tree(Okay, so I can't use git status with a bare repo; makes sense I guess)

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

git branch

(Nothing, it seems the bare repo doesn't contain any branches. Do I have to add them from a cloned repo?)

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

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

(I get a warning about cloning an empty repository)

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

cd test-repo

(The prompt changes to indicate I am on the master branch)

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

git branch

(Shows no results - eh?)

(显示没有结果 - 嗯?)

git branch master

fatal: Not a valid object name: 'master'

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

Um. So how do I create the master branch in my bare repo?

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

回答by jub0bs

A bare repository is pretty much something you only push to and fetch from. You cannot do much directly "in it": you cannot check stuff out, create references (branches, tags), run git status, etc.

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

If you want to create a new branch in a bare Git repository, you can push a branch from a clone to your bare repo:

如果要在裸 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

回答by chepner

A branch is just a reference to a commit. Until you commit anything to the repository, you don't have any branches. You can see this in a non-bare repository as well.

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

$ 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

回答by kubanczyk

No need for a second repository. We can supply a dummy directory using --work-treeoption and commands like checkoutand commitstart to work even on a bare repository. Prepare a dummy directory:

不需要第二个存储库。我们可以使用--work-tree选项和命令提供一个虚拟目录,即使在一个裸存储库上checkout也可以commit开始工作。准备一个虚拟目录:

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

And there you go:

然后你去:

$ 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

Tested on vanilla git 1.9.1. I didn't verify if posh-git supports --allow-emptyto make a commit without any file changes (a message-only commit).

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

回答by Shivraaz

By default there will be no branches listed and pops up only after some file is placed. You don't have to worry much about it. Just run all your commands like creating folder structures, adding/deleting files, commiting files, pushing it to server or creating branches. It works seamlessly without any issue.

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

https://git-scm.com/docs

https://git-scm.com/docs