繁体   English   中英

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

[英]Adding old git history to new repository

我创建了一个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

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

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


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

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 ,但是我将newrepooldrepo替换oldrepo

编辑2

建议使用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.

得益于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 ,用整个本地修改的分支替换了整个远程分支。 由于我的存储库位于gitlab中,因此master分支受到了保护,并且--force是不允许的。 为了使该命令成功执行,我不得不暂时取消保护master。

然后我继续每个分支:

git checkout --track origin/upstream
git rebase --onto original/upstream 6210d0b3cf20f506ce0bab7849550b38c9effb15    
# Manual resolution of conflict    
git rebase --continue
gir push --force origin upstream

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

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

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

是的,您应该能够为此使用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]

可能有冲突。

暂无
暂无

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

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