9

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?

user3207838
  • 676
  • 1
  • 7
  • 15
  • 1
    @JonHanna If I understood correctly, there were three developers: first that had a git repository, second one that didn't use a git repository and the OP that started a new git repository from the second developer state. Now he wants to merge first repo into his final repo. Is that right? – Ionică Bizău Nov 12 '15 at 10:58
  • @IonicăBizău ah, I think I see what I missed before (and +1 to your answer in response to it). – Jon Hanna Nov 12 '15 at 11:00

2 Answers2

7

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).

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • It seems that correct way to merge is `git merge -s ours --allow-unrelated-histories old-project/master`. But that does not remove the gap: it is impossible to apply `~1` to the first commit of new repo. – Egor Skriptunoff Oct 19 '16 at 12:32
-1

You may try to add old repository as remote repository at your new repository then pull changes to merge, assuming your repositories will be the baseline.

$> git remote add old /path/to/other/gitrepo/.git

# if you want to merge their master branch then pull from it
#   otherwise specify the correct branch to merge
$> git pull old master
  • The other way around surely? Clone the old repo, then pull all the changes from the new one. – Holloway Nov 12 '15 at 11:02
  • This does not create a "good" history that shows the evolution of the code. Yes, it combines all the commits into one graph; but it does not show the old history as ancestor of the new history, and in fact will often ignore the commits brought in by the "ours" merge (because they seem irrelevant to git). Instead consider options like these: https://stackoverflow.com/questions/44777043/git-copy-history-of-file-from-one-repository-to-another/44782681#44782681 – Mark Adelsberger Nov 09 '18 at 04:24