将旧的git历史记录添加到新的存储库

时间:2018-11-14 18:31:50

标签: git

我创建了一个git存储库并将内容提交给它。

git clone git://newrepo.com/project.git # Empty repository
tar -xzvf project-0.6.tar.gz -C project     # Fill it with tar contents
cd project
git commit -a
... Do work
... Commit
... Push

但是后来发现具有git历史记录的同一项目的恢复版本,是使用tar.gz的早期版本制作的。

如何将历史记录从旧项目导入到新项目中?


我在想我需要做这样的事情:

git clone git://newrepo.com/project.git                 # Start fresh
cp -r project project_backup                            # Backup the content
cd project                               
git init                                                # Wipe everything
git remote set-url origin git://oldrepo.com/project.git # Point to the old project
git pull                                                # Get clean history
cp -r ../project_backup .                               # Apply new content
git commit -a                                           # One big commit
git remote set-url origin git://newrepo.com/project.git # Point to the new project
git push                                                # Push changes

但是我不认为git init会那样工作。 man说它实际上并没有触及git历史记录。我听说git rebase可能是解决方案,但是我不确定如何在这种情况下使用它。

git-manpage格式:我要打开它:

A---B---C master (oldrepo)

D---E---F master (newrepo)

进入

A---B---C---D'---E'---F' master (newrepo)

编辑:

要做具有对oldrepo的推送访问权限。我确定我可以弄清楚如何将更改应用于oldrepo,但是如何将newrepo替换为oldrepo

Edit2

建议使用

git rebase。我有问题。当我解决冲突并完成变基后,

git clone git@newrepo.com/project.git project-rebase
cd project-rebase
git fetch --all
git remote add original https://oldrepo.com/project-old.git
git fetch original
git rebase --onto original/master 6210d0b3cf20f506ce0bab7849550b38c9effb15 master
--- resolving conflict ---
git rebase --continue

在这一点上,我希望我们能完成。 git log看起来不错,但是:

$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 102 and 11 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean'

$ git push origin master
To newrepo.com:project.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@newrepo.com:project.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

3 个答案:

答案 0 :(得分:1)

将旧仓库添加为远程仓库,签出远程分支(为其创建本地分支或在分离的HEAD上工作)....或非常类似于您用于创建的tar的修订版本项目的第一个修订版本。...在将tar内容提交到您正在处理的分支之上后,选择修订版本。

答案 1 :(得分:1)

是的,您应该可以使用git rebase

将旧存储库添加为远程存储并获取。

git remote add original <directory or url>
git fetch original

现在,您就像...

A - B - C [original/master]

D - E - F [master]

然后在此基础上重新建立工作。您应该在添加了tarball的新仓库中跳过第一次提交,在此处提交D,否则将还原您的旧工作。

git rebase --onto original/master D master

这将从(但不包括D(直至并包括master)中重建基准。因此只有E和F是您在源tarball顶部的补丁。

          E1 - F1 [master]
         /
A - B - C [original/master]

可能有冲突。

答案 2 :(得分:0)

多亏了Schwern的出色开始,问题得以解决。这是整个解决方案:

git clone git@newrepo.com:project.git
cd project
git fetch --all
git remote add original https://oldrepo.com/project-old.git
git fetch original
git rebase --onto original/master 6210d0b3cf20f506ce0bab7849550b38c9effb15 master

# Manual resolution of conflict

git rebase --continue
git push --force origin master      # Need to disable branch protection in gitlab
                                    # Re-enable protection in gitlab

我缺少--force,该--force将整个远程分支替换为您本地修改的分支。由于我的存储库位于gitlab中,因此master分支受到了保护,并且不允许git checkout --track origin/upstream git rebase --onto original/upstream 6210d0b3cf20f506ce0bab7849550b38c9effb15 # Manual resolution of conflict git rebase --continue gir push --force origin upstream 。为了使该命令成功执行,我不得不暂时取消保护master。

然后我继续每个分支:

git checkout --track origin/pristine-tar
git rebase --onto original/pristine-tar 6210d0b3cf20f506ce0bab7849550b38c9effb15
git push --force origin pristine-tar

我的最后一个分支没有冲突,所以这个很简单:

...
TypeBase b = null;
MethodDoingStuff(passedID, ref b); 
RepositoryCall(b, otherArgs);