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

五迷三道 提交于 2020-01-06 20:16:51

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/30245460/join-old-and-new-repo-created-by-copying-files-not-cloning-preserving-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!