如何在 Git 中创建没有父级的提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5689960/
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
How do I create a commit without a parent in Git?
提问by user648855
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.
我有一个没有任何版本控制软件的小项目,我最终得到了同一个脚本的三个不同版本,其中每个版本都与其他版本略有不同。我用其中一个版本初始化了一个 git repo,我真的很想在没有父母的情况下提交另外两个版本。这样,我可以合并差异,就好像脚本的三个版本中的每一个都是不同分支的提示一样。
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.
我什至不确定这是否一定是解决这个问题的理智或合理的方法......关于如何以这种方式做事的任何建议,或关于处理这种情况的更好方法的建议,将不胜感激。
回答by manojlds
From my comment:
从我的评论:
Why don't you create two branches and add the two other versions to them and then work on the branches?
为什么不创建两个分支并将另外两个版本添加到它们,然后在分支上工作?
Which I think would agree with ctcherry's answer as well.
我认为这也同意 ccherry 的回答。
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
:
但是,如果您确实想创建一个与 master 无关的完全不同的“分支”,那么您可以使用git checkout --orphan
. 该手册git checkout
介绍了该选项的工作--orphan
:
Create a new orphanbranch, 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.
创建一个新的孤立分支,名为 <new_branch>,从 <start_point> 开始并切换到它。在这个新分支上进行的第一次提交将没有父级,它将成为与所有其他分支和提交完全断开的新历史的根。
You might use it like this:
你可以这样使用它:
git checkout --orphan version2
git rm -rf .
<add version2 script>
git add your files
git commit -m 'Initial commit for version 2'
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:
如果您的 Git 早于 1.7.2(git checkout --orphan
引入时),那么您可以使用Git 社区手册中“创建新的空分支”中的备用命令序列:
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 symbolic-ref HEAD refs/heads/newbranch rm .git/index git clean -fdx <do work> git add your files git commit -m 'Initial commit'
回答by Philip Oakley
Why not start with an 'Empty' repo (e.g. a single readme of your problem statement above). Then you can branch the three times and add the different script versions into each of the branches, and commit them separately.
为什么不从“空”存储库开始(例如,上面问题陈述的单个自述文件)。然后可以进行3次分支,将不同的脚本版本添加到每个分支中,分别提交。
You can then branch/edit/commit/merge and try out all you options in a well managed way.
然后,您可以分支/编辑/提交/合并并以良好管理的方式尝试所有选项。
回答by ctcherry
You can create a branch for each version of the script, and commit them to those individual branches. Then merge them one at a time back into master. That should have the effect you are looking for.
您可以为每个版本的脚本创建一个分支,并将它们提交到这些单独的分支。然后一次将它们合并回母版。这应该有你正在寻找的效果。