加入通过复制文件(而不是克隆)创建的新旧存储库,保留提交历史记录

Join old and new repo, created by copying files (not cloning), preserving commit history

为了简化事情,我在 git old_repo 中有以下结构 "subpath":

subpath/old_commit_1
subpath/old_commit_2
subpath/old_commit_latest

我决定移动到 new_repo 并且只是将最新版本的 "subpath"(来自 old_commit_latest)复制到其中,没有任何提交历史。

所以 new_repo 现在有 "subpath" 和我对它所做的一堆新提交:

subpath/new_commit_subpath_added # added subpath here
subpath/new_commit_1
subpath/new_commit_2
subpath/new_commit_latest

现在我需要将所有历史记录从 old_repo 迁移到 new_repo 以获得 new_repo 中的以下树:

subpath/old_commit_1
subpath/old_commit_2
subpath/old_commit_latest
subpath/new_commit_1
subpath/new_commit_2
subpath/new_commit_latest

我该怎么做?

我只需要在master分支上做,但是在同样的情况下我那里有很多文件。 old_repo 中的子路径和文件名与 new_repo 中的匹配。

我想我需要为 old_repo 中的子路径创建补丁,回滚到 new_repo 中每个子路径的第一个提交,删除第一个提交,应用补丁然后重新设置所有新的承诺它。不知道如何做到这一切。将不胜感激。

准备中。

您应该将新旧版本都用作有效存储库。旧版本最好在本地机器上,新版本可以在本地或远程(例如,github)。

对存储库和项目进行备份总是一个好主意。

我假设旧版本的最后一次提交恰好是新版本的第一次提交。如果不是这样:

  1. 将较新版本重置为第一次提交。
  2. 删除旧版本项目文件夹中除.git之外的文件,但包括.gitignore和其他git设置。
  3. 将文件从较新版本的项目文件夹复制到较旧版本的项目文件夹。
  4. 将所有更改保存为 old/master 上的新提交。

加入存储库

# go to the old repo folder
cd path/to/old
# add the new repo as a remote
git add remote newrepo path/to/new/.git

#check that it's properly added
git remote show newrepo

#fetch data from new
git fetch newrepo

#create a branch "new", tracking
git checkout -b new newrepo/master

#merge changes to the old master
git checkout master
git merge newrepo

#an editor will open with a merge commit message. If you've done the preparations, there should be no merge conflicts.
#this should show a complete history now
git log --oneline

现在您在旧项目目录中有了联合历史记录和最新提交。