git - 设置一个没有rebase的commit的父级

时间:2010-11-12 10:54:21

标签: git rebase

我使用git-svn来创建SVN存储库的git镜像。 SVN内部的结构有点不合标准,因此git创建了一个没有master分支的公共提交的分支。

      A---B---C topic

D---E---F---G master

我知道提交A基于提交E而且我非常肯定我已经解决了导致git无法识别该事实的问题(使用filter-branch)。我想要做的是将topic重新附加到master分支,将E设置为A的父级:

      A---B---C topic
     /
D---E---F---G master

git-rebase似乎对我不起作用,因为提交A的差异列出了master中已存在的大量文件的创建,导致数量巨大冲突 根据我对git的理解,只需将E设置为A的父级就足以解决所有问题。
这可能吗?如果是,我该怎么办?

3 个答案:

答案 0 :(得分:29)

看看移植物(移植物文件可以在.git/info/grafts中找到)。格式非常简单:

<commit sha1> <parent1 sha1> <parent2 sha1> … <parentN sha1>

这使得git相信提交的父母与实际拥有的不同。使用filter-branch使移植物永久化(因此可以移除移植物文件):

git filter-branch --tag-name-filter cat -- --all

请注意,此重写了存储库的历史记录,因此不应在共享存储库中使用!


例如,如果您只想重写被移植到主分支上的提交的历史记录,请使用以下命令:

git filter-branch --tag-name-filter cat -- master..

答案 1 :(得分:8)

基于你的图表(虽然我很关心你的意思“我非常肯定我已经解决了导致git无法识别这一事实的问题(使用filter-branch)。”),你应该可以做类似以下的事情。

# checkout A
git checkout A

# Reset the branch pointer to E so that E is the parent of the next commit
# --soft ensures that the index stays the same
git reset --soft E

# Remake the commit with the E as the parent, re-using the old commit metadata
git commit -C HEAD@{1}

# Rebase the topic branch onto the modified A commit (current HEAD)
git rebase --onto HEAD A topic

答案 2 :(得分:6)

所有你需要的是:

git rebase --root --onto master^^ topic^^ topic

root选项允许您包含A。

更新:

如果要保留要变基的零件的分支和合并,请添加--preserve-merges选项。