0

I have one git repository of a library and another git repository of a project:

library/
project/

I need to copy the library to a folder inside the project:

project/ref/library/

Inside this project I will make modifications to the library specific to that project, thus it would not be viable to use submodules. I also need to preserve the history of this library.

As a bonus, I would like also to know how could I merge/rebase future developments of the library into my fork of the library inside the project.

Gabriel Diego
  • 950
  • 2
  • 10
  • 22
  • 2
    I would consider making a project-specific fork of the library, managing it like any other fork, and then using the fork as a submodule of your project repo. This avoids a lot of thorny "what happens when" and "how do I deal with" questions – Mark Adelsberger Jan 19 '18 at 16:39
  • @MarkAdelsberger Thanks for the suggestion, I will consider that too. – Gabriel Diego Jan 19 '18 at 16:41

3 Answers3

0

I found a solution to the problem based on this answer:

cd library/
mkdir -p ref/library/
git mv * ref/library # Remember to not include the ref/library itself 
git commit -m "moved to ref/library"
cd ../project/
git remote add library-repo ../library
git fetch library-repo
git branch library-branch remotes/library/master
git merge library-branch --allow-unrelated-histories
git remote rm library-repo

Once there is something new on the library, I can just go the library and do git fetch --rebase, then repeat the process.

Gabriel Diego
  • 950
  • 2
  • 10
  • 22
0

One way to get what you want and still rely on git submodules is to clone your library as a git submodule inside your project, and then create a new branch specific to your project inside the library.

This way, you get all changes from the library whenever you do a merge/rebase on your branch, you do not have to push your commits back to the original library, and you can perform as many changes as you need in your branch to fit your project's needs.

gchelfi
  • 89
  • 5
  • But how would I save my changes in the submodule back to the remote? @MarkAdelsberger gave the suggestion of forking the library and use the fork as submodule. I particularly don't like much submodules since I have to make separate commits for the submodule and the main project and I always have to do git pull in each submodule separately. – Gabriel Diego Jan 19 '18 at 17:22
0

I would suggest add library as submodule to project using (inside project repo):

git add submodule library.git ref/library

This will make it possible to code the changes inside ref/library, but also decide when to take in changes in ref/library.

The submodule command has a foreach sub command that can make it easier to pull new things into the submodules.

git submodule foreach git pull || :

Will run git pull in each submodule and continue if you get an error in one submodule, because of the || :

Good luck!

Jan Lovstrand
  • 228
  • 2
  • 9