85

I've been using SVN for all my projects. Sometimes project B is originating as a copy from project A. When project A has a generic change, I can use svn merge A within directory B and it will merge those changes.

Now, if I wanted to use git. I don't like having all my projects in the same repository since I then have to clone everything and can't pick just one project like in SVN. But having one repository for each project, how do I go about doing the same like I did earlier with SVN?

The question is: What's the best way to structure it if I want several subprojects that really all relates to one original project and to keep them in sync? And that I also want to be able to check them out separately

baloo
  • 7,635
  • 4
  • 27
  • 35
  • 1
    You'd probably better have a look at git-submodule (http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html). – Cascabel Jun 01 '10 at 12:53
  • @Jefromi but the source files are basically from the same history, this would mean I have two copies of the files? I think submodule is more for keeping track of external libraries that you include as separate files (not merge)? – baloo Jun 01 '10 at 13:56
  • Never mind, I didn't see that you said B was a copy of A. Reading your "the question is", where you said "subprojects...relating to one original project...in sync" I assumed you meant taking out chunks of that original project, but keeping them in sync in the metaproject. – Cascabel Jun 01 '10 at 21:51

1 Answers1

155

If you have two projects, proj1 and proj2 and want to merge changes of proj1 into proj2, you would do it like this:

# in proj2:
git remote add proj1 path/to/proj1
git fetch proj1
git merge proj1/master # or whichever branch you want to merge

I believe this does the same thing as what you were doing with SVN.

Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
  • But will merge change #2 track that we already merged change #1 doing it this way? I had to re-merge all changes (and correct conflicts) for all changes every time a new change was added if I remember correct. This didn't happen the SVN-way – baloo Jun 01 '10 at 13:50
  • Of course, merge works as usual. You do not have to "remerge all changes". This is nothing more than the usual merge, the one that you would use with a distant server. – Olivier Verdier Jun 01 '10 at 14:12
  • What if I don't have a remote? I would like to do the same (merge projA into projB) but I'm working only locally without remotes. – L. Holanda Dec 23 '15 at 23:22
  • 2
    @LeoHolanda Local files count as remote as far as git is concerned. – PyRulez Jan 09 '16 at 17:53
  • 4
    @LeoHolanda You have point here. I had to denote local repo in URL format, such as file:///Users/abc/path/to/my/repo/.git – mcku Jan 23 '16 at 22:55
  • 3
    I'd suggest to use `--allow-unrelated-histories` flag in merge to allow merge histories that do not share a common ancestor (https://git-scm.com/docs/git-merge). – Dariusz Woźniak Aug 09 '19 at 07:48
  • 2
    Remotes can be just paths: `git remote add r1 ../r1`. `--allow-unrelated-histories` is not recommended, it's [required](https://github.com/git/git/blob/v2.26.2/Documentation/RelNotes/2.9.0.txt#L58-L65) since `git >= 2.9` to merge branches from different repositories. – x-yuri May 30 '20 at 01:19