Tomasz Grobelny
Tomasz Grobelny

Reputation: 2746

add history to git repository or merge git repositories

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.

Upvotes: 9

Views: 3958

Answers (2)

Jean Paul
Jean Paul

Reputation: 1578

This is how I did, not sure it is the correct way to do but at least that worked for me:

git clone url:/to/new
cd repo
git remote add old url:/to/old
git remote update
git rebase --committer-date-is-author-date old/master

That was it. Now if there are other branches maybe you will also need to rebase them on master, which may not be so trivial.

The git history of the new repo became flat but for me that was OK when looking at git log -p --graph

The major issue was that I became the committer of all the commit of the new repo. To avoid that, you can look at the answer to a similar question which uses git filter-branch --parent-filter instead of git rebase: https://stackoverflow.com/a/44078243/4374441

Upvotes: 0

p91paul
p91paul

Reputation: 1142

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)

Upvotes: 3

Related Questions