add history to git repository or merge git repositories

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

Everybody understands something different by "merge git repositories", so here is my case. We had a TFS repository and we have checked out the sources at one point in time and made an initial git commit. Then we did normal development in git (branching, merging, etc.). The problem is that in the new repository we do not have history in out git repository and we would like to fix that. Therefore I have converted whole TFS repository to git repository and now I need to merge the converted TFS repository with the current git repository.

As becomes obvious from the above description the repositories are independent from git standpoint, but from logical point of view they have one commit in common (the commit that became initial commit to current git repository).

How do I merge those repos without losing history of any of them? I could just take the converted TFS repo as base and then cherry pick changes from master of the current repo, but that would not import all the branches that were created in the current repo.

回答1:

EDIT: my previous answer does not lead to nothing good. git rebase was not meant to do this. However, something similar already happened on stackoverflow: Merging two git repositories to obtain linear history

You can try with this procedure:

git init combined cd combined git remote add old url:/to/old git remote add new url:/to/new git remote update 

You will have a new repo, with references to both repos. then

git reset --hard old/master 

This will make the master branch point to the master branch of the old repo. You can now cherry pick all commits from the master branch of new repo

git cherry-pick <sha-1 of first commit on new repo>..new/master 

We are were we started: master branch is ok, but what about other branches in the new repo? Well, you do not need your old repo anymore, so

git remote rm old 

then, say you have a branch named 'branch1' in your new repository.

git checkout branch1 git rebase master 

This should change history of branch1 to make it start from master (the combined master, containing history from your imported repo), and rebasing should happen without conflicts. Check history is consistent with gitk, then you can force push with

git push -f new master git push -f new branch1 

You have to be sure that history is ok before forcing push, since this will change history upstream (keep a backup of both new and old repos to recover if needed)



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