如何在Git中创建没有父项的提交? [英] How do I create a commit without a parent in Git?

查看:137
本文介绍了如何在Git中创建没有父项的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

在git中,是否有一种将不相关的分支引入资源库的简单方法?

我有一个我正在开发的小项目,没有任何版本控制软件,最后我用了三个同一个脚本的不同版本,其中每个脚本与其他脚本略有不同。我使用其中一个版本初始化了一个git回购,我真的很想在没有父母的情况下犯下其他两个版本。这样,我可以合并这些差异,就像脚本的三个版本中的每一个都是不同分支的提示一样。

I have a small project I was working on without any kind of version control software, and I ended up with three different versions of the same script, where each one does something slightly different from the others. I initialized a git repo with one of the versions, and I'd really like to commit each of the other two without parents. That way, I can merge the differences as though each of the three versions of the script were the tips of different branches.

我甚至不确定这是否必要一个理智的或合理的方式去解决这个问题......任何关于如何做事的建议,或更好的方式来处理这种情况的建议,将不胜感激。

I'm not even sure whether this is necessarily a sane or reasonable way to go about solving this problem... Any advice on how to do things this way, or advice on a better way to handle this situation would be much appreciated.

推荐答案

从我的评论:


为什么不创建两个分支并添加还有两个版本给他们,然后在分支上工作?

Why don't you create two branches and add the two other versions to them and then work on the branches?

我认为这也与ctcherry的答案一致。

Which I think would agree with ctcherry's answer as well.

但是,如果您确实想创建一个与主控无关的完全不同的分支,那么您可以使用 git checkout --orphan 。针对 git checkout 手册描述选择 - orphan

But if you really do want to create a completely different "branch" that is unrelated to master, then you can do it with git checkout --orphan. The manual for git checkout describes to work of the option --orphan:

创建一个新的名为< new_branch>的孤立分支从< start_point>开始。并切换到它。在这个新分支上进行的第一次提交将没有父母,它将成为与所有其他分支和提交完全断开的新历史记录的根。

Create a new orphan branch, named <new_branch>, started from <start_point> and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

您可以像这样使用它:

git checkout --orphan version2
git rm -rf .
<add version2 script>
git add your files
git commit -m 'Initial commit for version 2'



<如果你的Git早于1.7.2(当引入 git checkout --orphan 时),那么你可以使用
Creating New Empty Branches创建 Git Community Book

If your Git is older than 1.7.2 (when git checkout --orphan was introduced), then you can use the alternate command sequence from "Creating New Empty Branches" in the Git Community Book:


偶尔,您可能希望在存储库中保留分支不要与你的普通代码分享祖先。这些的一些例子可能是生成文档或者沿着这些方面的东西。如果你想创建一个不使用当前代码库作为父代的新分支头,你可以创建一个如下所示的空分支:

Ocasionally, you may want to keep branches in your repository that do not share an ancestor with your normal code. Some examples of this might be generated documentation or something along those lines. If you want to create a new branch head that does not use your current codebase as a parent, you can create an empty branch like this:

git symbolic-ref HEAD refs/heads/newbranch 
rm .git/index 
git clean -fdx 
<do work> 
git add your files 
git commit -m 'Initial commit'


这篇关于如何在Git中创建没有父项的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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