Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a git repository with all its history into an existing git repository

Tags:

I have two git repositories and I want to merge them together without losing their commit histories. I've tried this:

cd firstRepo git remote add other path/to/otherRepo git fetch other git checkout -b otherRepoBranch other/master echo "`git rev-list otherRepoBranch | tail -n 1` `git rev-list master | head -n 1`" >> .git/info/grafts git rebase otherRepoBranch master 

Now when I look at the commit history everything looks good, but the only files I have in my repository are now the ones from otherRepo.

Any ideas?

like image 278
Curious Attempt Bunny Avatar asked May 13 '11 17:05

Curious Attempt Bunny


2 Answers

I take it the git repositories are unrelated? As in they're distinct repositories for separate projects that you want to be merged together to form a combined repository? If so, then it's possible the following references may help:

Combining multiple git repositories.

Merging two unrelated repositories.

like image 124
Phil Street Avatar answered Oct 15 '22 07:10

Phil Street


This definitive article addresses the simple case in the opening paragraph -- and addresses the more likely case as its primary topic: How to use the subtree merge strategy

The commit history of both repositories is preserved.


Here's my version -- based on the above referenced article...

git remote add temp staging_path/(reponame) git fetch temp git fetch --tags temp ## optional -- may pull in additional history for remote in $(git branch -r | grep temp/ ) ; do git branch --no-track imported_$(basename $remote) $remote ; done ## create local branches prefixed with 'imported_' git remote rm temp ## optional -- assumes you no longer plan to use the source repo  git merge -s ours --no-commit imported_master ## mysterious "merge strategy" git read-tree -u --prefix=(reponame)/ imported_master ## move to sub-folder git commit 
like image 38
Brent Bradburn Avatar answered Oct 15 '22 06:10

Brent Bradburn