在git的空项目上创建分支 [英] Creating branches on an empty project in git

查看:190
本文介绍了在git的空项目上创建分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  git init 



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

  git分支file1_branch 
git分支file2_branch
git分支file3_branch


$ b


fatal:不是有效的对象名称:'master'。


这是为什么?



也许这个问题可能与这个问题有关,即使是master分支也没有创建。我试着做一个 git分支。它没有任何效果。

然后我想了一下做一个空的提交来强制git创建 master 分支:

  git commit -m`initial_commit` 

但是因为我没有将任何文件添加到临时区域,所以它不起作用。



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



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



谢谢

解决方案

作为指向该分支中最新提交的指针。如果尚未创建提交,那么该分支没有指向任何内容。所以你不能真正创建分支,直到你至少有一次提交。



Git表示提交作为一个或多个父提交的引用(或者全零父代if这是初始提交),提交消息和一些其他元数据以及树引用。树基本上是每个目录中每个文件内容的清单。如果你的目录是空的,那么树中就没有东西了。现在,实际上可以创建一个空的提交作为第一次提交;你可以运行 git commit --allow-empty -minitial commit。但这并不是真正使用Git的常用方式。



在Git中,除非需要它们,否则预计不会使用分支。除非您有一些内容需要分支,否则不需要创建多个分支。如果你在开发的一开始就创建了三个分支,你们之间没有共同的祖先吗?在那种情况下,为什么不创建三个回购协议?或者你是否要同步更新所有三个,直到他们开始分歧?这似乎是很多额外的工作,以保持所有这些更新;你应该等待创建它们直到它们需要分歧为止。

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

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

解决方案

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.

这篇关于在git的空项目上创建分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆