git - 设置提交的父级而不进行变基

发布于 2024-10-02 10:57:50 字数 653 浏览 6 评论 0原文

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

      A---B---C topic

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

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

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

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

I used git-svn to create a git mirror of an SVN repository. The structure inside the SVN was a little off-standard, so git created a branch that has no common commit with the master branch.

      A---B---C topic

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

I know that commit A is based off commit E and I'm pretty positive that I've fixed the issues causing git not to recognize that fact (using filter-branch). What I want to do is re-attach topic to the master branch, setting E as the parent of A:

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

git-rebase doesn't seem to work for me because the diff for commit A lists the creation of a whole lot of files that already exist in master, resulting in a huge number of conflicts.
From my understanding of git just setting E as the parent of A should be enough to solve all problems.
Is this possible? If it is, how can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不弃不离 2024-10-09 10:57:50

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

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

这使 git 相信提交具有与实际不同的父级。

在较新版本的 Git 中,您需要使用 替换机制将历史移植到一起:

git Replace --graft ...

无论选择哪种方法(移植文件或替换引用),都使用过滤器分支使移植永久化(因此可以删除移植文件):

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

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


例如,如果您只想重写正在嫁接到 master 分支上的提交的历史记录,请使用以下命令:

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

Have a look at grafts (the graft file can be found in .git/info/grafts). The format is pretty simple:

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

This makes git believe that a commit has different parents than it actually has.

In newer versions of Git, you want to use the replace mechanism to graft histories together:

git replace --graft …

Regardless of chosen approach (graft file or replace refs), use filter-branch to make grafts permanent (so the grafts file can be removed):

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

Note that this rewrites history of the repository, so should not be used on shared repos!


If you only want to rewrite the history of the commits that are being grafted onto the master branch, for example, use this command:

git filter-branch --tag-name-filter cat -- master..
伏妖词 2024-10-09 10:57:50

根据你的图表(尽管我担心你所说的“我非常肯定我已经解决了导致 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

Based on your diagrams (although I'm concerned about what you mean by "I'm pretty positive that I've fixed the issues causing git not to recognize that fact (using filter-branch)."), you should be able to do something like the following.

# 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
囚我心虐我身 2024-10-09 10:57:50

您所需要的只是:

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

root 选项允许您包含 A。

更新:

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

All you need is this:

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

the root option lets you include A.

UPDATE:

Add the --preserve-merges option if you want to retain the branching and merging of the part that you are rebasing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文