有没有办法在保持现有提交哈希的基础上进行git rebase? [英] Is there a way to do git rebase with keeping the existing commit hashes?

查看:64
本文介绍了有没有办法在保持现有提交哈希的基础上进行git rebase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下一个方案,其中一个项目具有您对100%满意的现有git树.现在,您发现了一些早于迁移到git的古老源代码,并且希望使其成为git历史的一部分.

Imagine a scenario where a project has an existing git tree you're 100% satisfied with. Now you discover some ancient source code predating migration to git and would like to make it a part of git history.

有很多方法可以实现这一目标,但据我所知,它总是以新的git树结尾,即所有现有提交都具有不同的ID.

There are many ways how to achieve this but to my knowledge it always ends up with a new git tree, i.e. where all the existing commits have different IDs.

在这种特定情况下,似乎没有必要,除了第一个提交的父级之外,我没有更改任何其他内容.有办法吗?

In this specific case it doesn't seem to be necessary, I'm not changing anything but the first commit's parent. Is there a way to do this?

推荐答案

不,从根本上讲这是不可能的.提交的ID是其合并内容的哈希值.这不仅包括整个树和文件的内容,还包括提交消息,作者信息,对其父项的引用.

No, this is fundamentally impossible. A commit’s id is the hash of its combined content. That includes not only the whole tree and file content, but also the commit message, author information, and the reference to its parent.

因此,通过更改提交的父代,您可以更改其内容,从而使其先前的ID无效.为了将提交整合到历史中,Git将不得不重新计算其哈希值.否则,它将拒绝该提交被破坏,并使您的存储库处于破坏状态.

So by changing the parent of a commit, you are changing its content and as such invalidate its previous id. Git will have to recalculate its hash in order to integrate the commit into the history. Otherwise it would reject that commit as being broken and leave your repository in a broken state.

任何提交ID与其内容的哈希匹配,并且对于任何直接或间接父级都是如此,这是Git完整性的核心部分.你无法避免这一点.

The fact that any commit id matches the hash of its content, and that this is true for any direct or indirect parent is a core part of Git’s integrity. You cannot avoid this.

因此,不行,您不能在不影响提交哈希的情况下完成所需的操作.您可能要做的就是简单地添加另一个与您当前分支没有任何关系的完全不相关的分支.这样,您将不会影响现有的提交,但是您也可以将旧的历史记录集成到存储库中,以便将其存储在内部(不是集成的,但至少在其中).

So no, you cannot do what you want without affecting commit hashes. What you maybe could do is simply add another completely unrelated branch that has no connection to your current branches. That way you wouldn’t affect your existing commits but you would also have a way to integrate that old history into the repository so it would be stored inside—not integrated but at least it’s there.

这篇关于有没有办法在保持现有提交哈希的基础上进行git rebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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