35

We have a project that was forked a while back mostly via a copy/paste method. But there is still significant overlap between the repos.

Is it possible to retroactively mark this fork as a fork to github, so that functions like compare and pull requests will do the right thing?

Note: I have tried the "hack" below, of forking anew, cloning the fork, copying the "forked" content over, then git add/commit/push. However, the file histories in the original "fork" are lost, and branches don't come over.

karlos
  • 3,965
  • 3
  • 16
  • 19
  • 1
    As a hack you can just fork one repo in github and then upload your forked version to replace new fork in github. Though this will cause an additional commit. – Shoaib Shakeel May 27 '15 at 04:24
  • possible duplicate of [Add Github fork to existing repository](http://stackoverflow.com/questions/7210273/add-github-fork-to-existing-repository) – jub0bs May 27 '15 at 07:22
  • 2
    @ShoaibShakeel The OP could always force push his code to that fresh fork; I know it's frowned upon, but if done quickly enough and on a brand new fork that hasn't yet deviated from the original repo, I don't see any harm. – jub0bs May 27 '15 at 07:24
  • Maybe this could help you: https://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories – creyD Jul 31 '19 at 07:16
  • 1
    Hi, did you consider accepting an answer, if one did help you? – creyD Nov 05 '20 at 18:53

3 Answers3

6

As you don't want to "squash" your commits into a single file what you could do is:

  • Fork the "original" repository
  • Create a pull request from your "copy and paste" repo to the fork

Or you could take a look at how to merge two repositories like in this post. Like they did there you could add your "c&p repo" to the fork as a subtree, like it is explained in detail over here.

This are the steps taken in the guide:

git remote add -f c&prepo /path/to/c&prepo
git merge -s ours --no-commit c&prepo/master
git read-tree --prefix=vendor/c&prepo/ -u c&prepo/master
git commit

However even with those methods you aren't able to merge the whole repositories but only specific branches. What you could do is i.e. create a pull request (described in my first method) for each branch, which could take up some time depending on your branching model.

I don't think there actually is a solution where you can merge two different repositories while keeping their commit history and branches at the same time. Best of luck to you.

creyD
  • 1,972
  • 3
  • 26
  • 55
4

you should try something along the lines for this:

  • create a real fork of the original project in github;
  • set this up as a remote of your copy/pasted fork (git add remote ...)
  • then rebase your work on top of this (git rebase)
Frido Emans
  • 5,120
  • 2
  • 27
  • 42
1

As far as I know, there's no way to do this retroactively; however, it is possible by pushing to a new fork.

  1. Create a new fork on GitHub
  2. git checkout deviant-branch
  3. git remote add Fork https://github.com/path-to-new-repo.git
  4. git push --force --set-upstream Fork/main-branch deviant-branch

Force-pushing is the only way to get a literal, unchanged copy of your current branch as the "main" repo in the fork. The other answers telling you to PR or rebase will not accomplish what you want.

All other branches should come over as a part of the new fork. Delete the ones you don't need.

Miles B Huff
  • 98
  • 1
  • 2
  • 9