简体   繁体   English

git 历史消失了 - 如何在创建新项目后将旧的 git repo 迁移到新的

[英]git history gone - How to migrate older git repo to the new one after creating new project

We have the following case:我们有以下案例:

  1. We have an older project P1 on older git Repo G1我们在旧的 git Repo G1 上有一个旧的项目 P1
  2. After a while we have created new project P2 by copying the older one P1 into the new new git Repo G2 and added further stuff on P2 and pushed to G2一段时间后,我们通过将旧的 P1 复制到新的新 git Repo G2 并在 P2 上添加更多内容并推送到 G2 来创建新项目 P2
  3. After a while we have noticed that we need the git History from older project P1 in the new project P2一段时间后,我们注意到我们需要新项目 P2 中旧项目 P1 的 git History

What is the easiest way to get git Histroy from G1 to G2 without losting the new changes on G2?在不丢失 G2 上的新更改的情况下,将 git Histroy 从 G1 获取到 G2 的最简单方法是什么?

I assume you just copied the contents of G1 and created a new repo from scratch, otherwise you would have a history.我假设您只是复制了 G1 的内容并从头开始创建了一个新的存储库,否则您将拥有历史记录。

If so rebasing G2 on top of the old G1 would do it.如果是这样,在旧的 G1 之上重新设置 G2 就可以了。

I tested the provided scenario with one of my projects and it results in the desired effect.我用我的一个项目测试了提供的场景,它产生了预期的效果。

1. In G2, add the G1 as a remote 1.在G2中,添加G1作为遥控器

git remote add G1 <Path>

2. Pull G1 2.拉G1

git pull G1 main

3. Rebase G2 on top of G1 3. 在 G1 之上重新设置 G2

git rebase G1 main

At this point, if you may get a message:此时,如果您可能会收到一条消息:

Current branch main is up to date.当前分支 main 是最新的。

If so, it is safe to rebase, so:如果是这样,rebase 是安全的,所以:

git rebase -f G1 main

Done完毕

Notice that G2 will lose its initial commit, as now the initial commit will be the one from G1.请注意,G2 将丢失其初始提交,因为现在初始提交将是来自 G1 的提交。 No problem because the contents should be the same.没问题,因为内容应该是相同的。

Inspect/Test the project, make sure everything is ok.检查/测试项目,确保一切正常。

If it is all good, force push G2, as the rebased commits will have their hash updated.如果一切顺利,强制推送 G2,因为重新提交的提交将更新其哈希值。

git push -f

Be careful if working on a team, just make sure no one has pending changes to push, ask people to drop then clone the repo again after this operation.在团队中工作时要小心,只要确保没有人有待推送的更改,要求人们删除然后在此操作后再次克隆 repo。 As this is a one-time thing it should be ok.由于这是一次性的事情,所以应该没问题。

Remove G1 remote移除 G1 遥控器

git remote remove G1

Tests测试

In my scenario, this was the result:在我的场景中,结果如下:

* 0da12b8 (HEAD -> main) G2 Second Commit <--initial commit was lost
* f2dbe6e G1 Second Commit
* d93f003 G1 Initial commit

History starts from G1 initial commit.历史从 G1 初始提交开始。 Commits from G2 are applied on top of the last commit on G1, so their ancestors are reset at that point.来自 G2 的提交应用于 G1 上最后一次提交的顶部,因此它们的祖先在该点被重置。

Initial commit on G2 is lost, but it is ok because its contents should be the same as the last commit on G1. G2 上的初始提交丢失了,但没关系,因为它的内容应该与 G1 上的最后一次提交相同。

This approach assumes a clean copy from G1.这种方法假设 G1 是一个干净的副本。

Tip小费

To avoid this situation one may copy a project with history to a new repository like so:为了避免这种情况,可以将具有历史记录的项目复制到新的存储库,如下所示:

git clone <A repo URL> project

cd project

git remote remove origin

git remote add origin <New repo URL>

git push -u origin main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM