Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two git repositories of same project

Tags:

git

I am currently the only developer working on a project I took over from my predecessor. When I took over the project, it was not under source control. So I created a new git repository, made an initial commit with the state and worked on it since.

But recently I discoveered in a backup an ancient version of the same project, which actually is a git repository, however, as my predecessor knew nothing about git, he copied all files out of it and worked on them without source control.

So, I now have two git repositories, one contains all changes up to a certain date, the other all changes from a later date up until now. But in between, there is a gap without source control.

Is it possible to merge these two repository such that it seems that it always existed as one single repository, but with the changes made without source control as one big commit in between?

like image 417
user3207838 Avatar asked Nov 12 '15 10:11

user3207838


People also ask

Can I merge two Git repositories?

To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.

Can we merge two different repositories?

You can merge repository A into a subdirectory of a project B using the subtree merge strategy. This is described in Subtree Merging and You by Markus Prinz.

Can GitHub project have multiple repositories?

You can now manage multiple GitHub repositories under a single project, and since we've also reworked sprints, the Issue Manager, and Tickets, the benefits are huge!


1 Answers

Sure, using the ours strategy!

Follow the the next steps (lines starting with # are comments):

# Navigate to your updated project
cd your-updated-project

# Add a remote pointing to the other git repository 
# (it can be a local path or an url; I used a local path)
git remote add old-project ../path/to/the/old/git/repo

# Get the content from that repo
git fetch old-project

# Merge the the old repo master branch in your current branch,
# BUT keeping your recent changes (see -s ours)
git merge old-project master -s ours

If you want to merge another branch from the old git repo, do git merge old-project <branch-name> -s ours (replace <branch-name> with the branch name you want to merge).

like image 112
Ionică Bizău Avatar answered Sep 28 '22 12:09

Ionică Bizău