Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I append one git history to another?

I have a repository that contains the code for an old version of a website that is no longer being used on master. The site was completely redesigned in a new repository, and the code and history from that repository was copied over into a branch in the repo of the old site, let's call it new_site. I can't merge new_site into master, since the histories are entirely different.

I've investigated both submodules and subtree merges a bit, but both seem to only be relevant when you're trying to keep code from one project contained in a subdirectory of another project, not when you want to stick two working trees on top of each other.

At this point, I would like to have the old code remain where it is, and just append new_site on top of the current HEAD. Any idea how I can accomplish this, or alternate suggestions for cleaning up this mess without blowing away the old code?

like image 453
styger Avatar asked Feb 07 '14 08:02

styger


People also ask

How do I merge two git repository and keep history?

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.

How do I mirror an entire existing git repository into a new one?

You first have to get the original Git repository on your machine. Then, go into the repository. Finally, use the --mirror flag to copy everything in your local Git repository into the new repo.


2 Answers

The general idea is:

  • explained in "How to combine two separate unrelated Git repositories into one with single history timeline" (fetch + cherry-picking)
  • implemented in "Concatenate the history of two Git repositories?"

    git clone <git repository B url>
    cd <git repository B directory>
    git remote add repo-A-branch <git repository A directory>
    git fetch repo-A-branch master
    git remote rm repo-A-branch
    

Once you have imported the right branch from A, you can use graft point, and then git filter-branch to turn a grafted history into "real" history, to get rid of the graft point.

like image 139
VonC Avatar answered Oct 17 '22 17:10

VonC


I wrote this script a while ago, which handles a common case for this (append a repository's history to another repository in a subdirectory):

https://github.com/multi-io/utils/blob/master/git-append-repo

like image 36
Olaf Klischat Avatar answered Oct 17 '22 17:10

Olaf Klischat