当整个项目以 "first commit" 提交到全新的 repo 时,如何添加回原始项目历史记录
How to add back the original project history when the whole project was committed with a "first commit" to a brand new repo
由于当时经验不足,我将一个github repo1的代码复制到一个全新的git repo2中,并提交了一个“first commit”消息,然后推送到我的位桶中的远程。对此 repo2 进行了一些更改并添加了一些分支。
现在,一年后,我想将对原始 github repo1 的一些更新传递给 bitbucket repo2,但两个 repo 都有不相关的历史记录。
如何将原始的 repo1 历史记录添加到我的 repo2?
我认为有可能将我的整个 repo2(包括所有新分支)重新定位到 repo1 的头部(现在包括一些更新),但将结果保留在我的 repo2 中(可能在一个新分支中) ).但是,我不确定该怎么做。任何帮助将不胜感激。
您可以使用 git replace
.
将历史“缝合”在一起
这是一种在本地存储库中进行设置的方法:
- 将原始仓库添加为当前仓库的远程仓库,并获取其历史记录:
git remote add history https://github.com/some/repo
git fetch history
在您的本地历史记录中:记下您“首次提交”的 sha1(我们称之为 sha1-local
)
在history
的提交中:找出与你自己的“第一次提交”具有相同内容的提交,并标记它的sha1(我们称之为sha1-history
)
运行 :
git replace sha1-local sha1-history
从现在开始:多个 git 命令将查看您的历史记录,就像它与 history
的历史记录相结合一样。
例如,如果 feature
是该存储库中的一个分支,您可以 运行 :
git checkout -b feature history/feature
git rebase master
如果您想重写本地存储库中的所有提交并使替换永久化,请调用 git filter-branch
或 git filter-repo
:
# to rewrite only your local master branch :
git filter-branch --index-filter true -- master
# to rewrite all your local branches :
git filter-branch --index-filter true -- --branches
由于当时经验不足,我将一个github repo1的代码复制到一个全新的git repo2中,并提交了一个“first commit”消息,然后推送到我的位桶中的远程。对此 repo2 进行了一些更改并添加了一些分支。
现在,一年后,我想将对原始 github repo1 的一些更新传递给 bitbucket repo2,但两个 repo 都有不相关的历史记录。
如何将原始的 repo1 历史记录添加到我的 repo2?
我认为有可能将我的整个 repo2(包括所有新分支)重新定位到 repo1 的头部(现在包括一些更新),但将结果保留在我的 repo2 中(可能在一个新分支中) ).但是,我不确定该怎么做。任何帮助将不胜感激。
您可以使用 git replace
.
这是一种在本地存储库中进行设置的方法:
- 将原始仓库添加为当前仓库的远程仓库,并获取其历史记录:
git remote add history https://github.com/some/repo
git fetch history
在您的本地历史记录中:记下您“首次提交”的 sha1(我们称之为
sha1-local
)在
history
的提交中:找出与你自己的“第一次提交”具有相同内容的提交,并标记它的sha1(我们称之为sha1-history
)运行 :
git replace sha1-local sha1-history
从现在开始:多个 git 命令将查看您的历史记录,就像它与 history
的历史记录相结合一样。
例如,如果 feature
是该存储库中的一个分支,您可以 运行 :
git checkout -b feature history/feature
git rebase master
如果您想重写本地存储库中的所有提交并使替换永久化,请调用 git filter-branch
或 git filter-repo
:
# to rewrite only your local master branch :
git filter-branch --index-filter true -- master
# to rewrite all your local branches :
git filter-branch --index-filter true -- --branches