30

I want to merge two remote repositories in Git.

One is mainstream repository, which I do not have write permission. I want to track its master branch.

The other is maintained by us, I have full rights on it.

I want to track the mainstream code. At the same time, our modification would be recorded in my remote repository.

How do I do this?

random
  • 9,774
  • 10
  • 66
  • 83
Readon Shaw
  • 365
  • 1
  • 4
  • 7

1 Answers1

56

I would recommend:

  • cloning yourRemoteRepo (that way, you can easily pull/push from that repo)
  • adding mainstreamRepo as a remote and fetch its branch, then track the one which interest you

    git clone git://yourRemoteRepo
    git remote add mainStreamRepo http://mainStreamRepo
    git fetch mainStreamRepo
    git checkout -b mainStreamMaster mainStreamRepo/master
    git checkout master
    

From there, you can

  • merge mainStreamMaster to your master,
  • or rebase your master on top of mainStreamMaster (in order to integrate the full history of mainStreamMaster into your master branch)
  • then make some evolutions to master (or to a topic-specific branch) that you can push to yourRemoteRepo.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @TravisCunningham (http://stackoverflow.com/users/2487332/travis-cunningham) thank you for the edit ([incorrectly rejected](http://stackoverflow.com/review/suggested-edits/2317758)): I have added the name of the remote I initially forgot. – VonC Jun 14 '13 at 19:48
  • Is this possible w/o clone but only fetch and then merging on the remote references? Or does merging always require a checked out working directory? – hakre Jul 16 '18 at 07:10
  • @hakre yes, merging involves a working tree where a branch is checked out. So a clone is involved as well. – VonC Jul 16 '18 at 07:26
  • Thanks for the blazing fast reply. For Fast-Forward "merges" (better: reset) this could work, but I should verify first if possible. Thanks again. – hakre Jul 16 '18 at 09:40
  • https://saintgimp.org/2013/01/22/merging-two-git-repositories-into-one-repository-without-losing-file-history/ – Marian K. Mar 29 '21 at 22:27
  • with your branch pulled from remote you can push to either origin i.e. `git push mainStreamRepo HEAD:master` or `git push origin` – Daniel Oct 19 '21 at 17:20
  • @Daniel That is true indeed. In the context of this question, though, that would not be possible (since one of the two repositories is read-only) – VonC Oct 19 '21 at 18:02