git - 如何在 Git 中创建没有父项的提交?

标签 git

<分区>

我有一个没有任何版本控制软件的小项目,我最终得到了同一个脚本的三个不同版本,每个版本的功能都略有不同。我用其中一个版本初始化了一个 git repo,我真的很想在没有 parent 的情况下提交其他两个版本。这样,我就可以 merge 差异,就好像脚本的三个版本中的每一个都是不同分支的提示一样。

我什至不确定这是否一定是解决这个问题的明智或合理的方法......任何关于如何以这种方式做事的建议,或者关于更好的处理这种情况的建议都会很多赞赏。

最佳答案

来 self 的评论:

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

我认为这也同意 ctcherry 的回答。


但是如果你真的想创建一个与 master 无关的完全不同的“分支”,那么你可以使用 git checkout --orphan 来完成。 manual for git checkout描述选项 --orphan 的工作:

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 :

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 - 如何在 Git 中创建没有父项的提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5689960/

相关文章:

git - 关于使用 Git 的问题

git - 如何交互式(可视化)解决 SourceTree/git 中的冲突

git - msysgit bash shell-如何解决 "cannot find command"

git - 将当前 git 存储库转换为与 git-buildpackage 兼容的存储库

git - 在提交之前丢失了所有内容,是否有恢复的希望

git - 如何在 TortoiseGit 中默认启用 "include tags"推送?

java - IntelliJ 交互式 rebase 失败

git - 通过git更新github wiki文件

git - 多个存储库和 Heroku

git - Jenkins 凭据插件和 GIT 客户端不工作