在 git 中的空项目上创建分支

发布于 2024-11-01 09:38:45 字数 614 浏览 0 评论 0原文

假设我要在 3 个不同的文件(fileA、fileB 和 fileC)中为我的一个新项目实现 3 个不同的功能。

我想我只需要将我的(当前为空的)项目添加到 git:

git init

然后创建 3 个不同的分支:

git branch file1_branch
git branch file2_branch
git branch file3_branch

但这不起作用:

致命:不是有效的对象名称:“master”。

这是为什么呢?

也许问题可能与此时甚至还没有创建主分支有关?我尝试做一个 git 分支。结果什么也没产生。

然后我考虑做一个“空”提交来强制 git 创建 master 分支:

git commit -m `initial_commit`

但由于我没有向暂存区域添加任何文件,所以它不起作用。

请记住,我即将开始我的项目,所以此时我没有任何文件要添加到提交中!

如何解决这个问题?我做错了什么吗?

谢谢

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.

I thought I'd just need to add my (currently empty) project to git:

git init

and then create 3 different branches:

git branch file1_branch
git branch file2_branch
git branch file3_branch

but this doesn't work:

fatal: Not a valid object name: '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.

I then thought about doing an "empty" commit to oblige git to create the master branch:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哎呦我呸! 2024-11-08 09:38:45

Git 将分支表示为指向该分支中最新提交的指针。如果您尚未创建提交,则该分支没有任何可指向的内容。因此,在至少进行一次提交之前,您无法真正创建分支。

Git 将提交表示为对一个或多个父提交(如果这是初始提交,则为全零父提交)、提交消息和一些其他元数据以及树引用的引用。树基本上是每个目录中每个文件内容的清单。如果您的目录为空,则没有任何内容可放入树中。现在,实际上可以创建一个空提交作为您的第一次提交;您可以运行 git commit --allow-empty -m "initial commit" 。但这实际上并不是使用 Git 的常见方式。

在 Git 中,除非需要,否则不应使用分支。在有一些内容要分支之前,无需创建多个分支。如果你在开发之初就创建了三个分支,它们之间会不会没有共同的祖先?在这种情况下,为什么不只创建三个存储库呢?或者您打算同步更新所有三个,直到它们开始出现分歧?要让所有这些都保持最新状态,这似乎需要做很多额外的工作;你应该等到它们需要发散时再创建它们。

但如果您想使用此工作流程,可以运行 git commit --allow-empty -m "initial commit" 来创建一个空的初始提交。

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 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.

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.

But if you want to use this workflow, you can run git commit --allow-empty -m "initial commit" to create an empty initial commit.

芯好空 2024-11-08 09:38:45

您可以使用以下命令进行空的初始提交:

git commit --allow-empty

然后您可以根据需要创建分支。

You can do an empty initial commit with:

git commit --allow-empty

And then you can create your branches as you like.

鸠魁 2024-11-08 09:38:45

我总是通过提交一个空文件来初始化分支:

touch a

git add a

git commit -m'init'

然后它会自动创建分支“master”。
之后您可以删除文件“a”。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文