在 git 中的空项目上创建分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5678699/
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
Creating branches on an empty project in git
提问by devoured elysium
Let's say I am about to implement 3 different features in 3 different files (fileA, fileB an fileC) for a new project of mine.
假设我要为我的一个新项目在 3 个不同的文件(fileA、fileB 和 fileC)中实现 3 个不同的功能。
I thought I'd just need to add my (currently empty) project to git:
我以为我只需要将我的(当前为空的)项目添加到 git:
git init
and then create 3 different branches:
然后创建 3 个不同的分支:
git branch file1_branch
git branch file2_branch
git branch file3_branch
but this doesn't work:
但这不起作用:
fatal: Not a valid object name: 'master'.
致命:不是有效的对象名称:'master'。
Why is this?
为什么是这样?
Maybe the problem could be related that even the master branch wasn't created at this point? I tried doing a git branch
. It yielded nothing.
也许问题可能与此时甚至没有创建主分支有关?我尝试做一个git branch
. 它什么也没产生。
I then thought about doing an "empty" commit to oblige git to create the master
branch:
然后我考虑做一个“空”提交来强制 git 创建master
分支:
git commit -m `initial_commit`
but as I didn't add any files to the staging area it doesn't work.
但由于我没有向暂存区添加任何文件,因此它不起作用。
Keep in mind I'm just about to start my project, so at this point I don't have any files to add to the commit!
请记住,我即将开始我的项目,所以此时我没有任何文件要添加到提交中!
How to solve this issue? Am I doing something wrong?
如何解决这个问题?难道我做错了什么?
Thanks
谢谢
回答by Brian Campbell
Git represents branches as pointers to the latest commit in that branch. If you haven't created a commit yet, there's nothing for that branch to point to. So you can't really create branches until you have at least one commit.
Git 将分支表示为指向该分支中最新提交的指针。如果您尚未创建提交,则该分支没有任何可指向的内容。所以你不能真正创建分支,直到你至少有一个提交。
Git represents commits as reference to one or more parent commits (or an all-zeros parent if this is the initial commit), a commit message and some other metadata, and a tree reference. A tree is basically a manifest of what is in each file in each directory. If your directory is empty, there is nothing to put in the tree. Now, it is actually possible to create an empty commit as your first commit; you can run git commit --allow-empty -m "initial commit"
. But that is not really a common way of using Git.
Git 将提交表示为对一个或多个父提交(或全零父提交,如果这是初始提交)、提交消息和一些其他元数据以及树引用的引用。树基本上是每个目录中每个文件中内容的清单。如果您的目录为空,则树中没有任何内容。现在,实际上可以创建一个空提交作为您的第一次提交;你可以跑git commit --allow-empty -m "initial commit"
。但这并不是使用 Git 的一种常见方式。
In Git, you're expected not to use branches unless you need them. There's no need to create several branches until you have some content to branch. If you create three branches at the very beginning of development, will you have no common ancestry between them? In that case, why not just create three repos? Or are you going to be updating all three of them in sync until they start to diverge? That seems like a lot of extra work to keep all of them up to date; you should just wait to create them until they need to diverge.
在 Git 中,除非需要,否则不要使用分支。在您有一些要分支的内容之前,无需创建多个分支。如果在开发之初就创建了三个分支,它们之间会不会没有共同的祖先?在这种情况下,为什么不创建三个 repos?或者您是否要同步更新所有三个,直到它们开始出现分歧?这似乎需要做很多额外的工作来保持所有更新。您应该等待创建它们,直到它们需要发散。
But if you want to use this workflow, you can run git commit --allow-empty -m "initial commit"
to create an empty initial commit.
但是如果你想使用这个工作流,你可以运行git commit --allow-empty -m "initial commit"
来创建一个空的初始提交。
回答by Frank S. Thomas
You can do an empty initial commit with:
你可以做一个空的初始提交:
git commit --allow-empty
And then you can create your branches as you like.
然后您可以根据需要创建分支。
回答by JiangHui Xie
I always init branch by commiting an emptyfile:
我总是通过提交一个空文件来初始化分支:
touch a
git add a
git commit -m'init'
then it create branch "master" automatically. After that you can delete the file 'a'.
然后它会自动创建分支“master”。之后,您可以删除文件“a”。