加入通过复制文件(而不是克隆)创建的旧的和新的回购,保留提交历史记录 [英] Join old and new repo, created by copying files (not cloning), preserving commit history

查看:136
本文介绍了加入通过复制文件(而不是克隆)创建的旧的和新的回购,保留提交历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  subpath / old_commit_1 
subpath / old_commit_2
subpath / old_commit_latest

我决定搬到new_repo并且只是将最新版本的subpath(from old_commit_latest)复制到其中,而没有任何提交历史记录。



所以new_repo现在有了subpath和一些新的提交我做到了:

pre $ code $ subpath / new_commit_subpath_added#在此添加子路径
子路径/ new_commit_1
子路径/ new_commit_2
subpath / new_commit_latest

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

 子路径/ old_commit_1 
子路径/ old_commit_2
子路径/ 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. 将所有更改另存为旧/主文件上的新提交。 >

    加入存储库



     #转到旧的回购文件夹
    cd path / to / old
    #添加新回购作为远程
    git添加远程newrepo路径/ to / new / .git

    #检查它是否已正确添加
    git remote show newrepo

    #从新
    获取数据git fetch newrepo

    #创建一个分支new,跟踪
    git checkout -b new newrepo / master

    #merge更改为旧主
    git checkout主
    git merge newrepo

    #an编辑器将以合并提交打开信息。如果你已经做好准备,那么不应该有合并冲突。
    #此应该显示完整的历史现在
    git log --oneline

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


    To simplify things I have the following structure in git old_repo for "subpath":

    subpath/old_commit_1
    subpath/old_commit_2
    subpath/old_commit_latest
    

    I decided to move to a new_repo and just copied the latest version of "subpath" (from old_commit_latest) into it without any commit history.

    So the new_repo now has "subpath" and a bunch of new commits I made to it:

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

    Now I need to migrate all the history from old_repo to the new_repo to get the following tree in 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
    

    How do I do that?

    I only need to do it to master branch, but I have lots of files there in the same situation. And subpath and filenames in old_repo matches the ones in new_repo.

    I guess I need to create patches for subpaths in old_repo, rollback to the first commit for each subpath in the new_repo, remove that first commit, apply patches and then rebase all the new commits over it. Not sure how to do it all. Will appreciate some help.

    解决方案

    Preparations.

    You should have both new and old versions available as valid repositories. The old version should better be on your local machine, new can be on local or remote (say, github).

    It's always a good idea to have backups of both repos and projects.

    I assume that the last commit of the old version is exactly the first commit of the new. If that's not so:

    1. Reset the newer version to its first commit.
    2. Delete the files in older version’s project folder except .git, but including .gitignore and other git settings.
    3. Copy the files from newer to older version project folder.
    4. Save all changes as a new commit on old/master.

    Joining repositories

    # 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
    

    Now you have the joint history and latest commits in the old project directory.

    这篇关于加入通过复制文件(而不是克隆)创建的旧的和新的回购,保留提交历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆