Question

I have a specific problem in git which I havent found an answer to yet. In gitlab I have 3 seperate repos. For my school project the teacher wants me to copy everything into a repo of his and wants to see all my git history.

So I would need to fork my 3 repos into his main repo, but I just couldnt figure out an answer.

Was it helpful?

Solution

There are three approaches you might consider for merging three repositories into a single repository. For clarity, I'll call your three repos A, B, and C, and the combined repository will be called teacher.

One repository, multiple branches

One repository can have multiple branches, not just master. As far as Git is concerned there is not a huge difference between maintaining multiple repositories or multiple branches. So what you can do is the following:

  1. create the teacher repository
  2. add the teacher repository as a “remote” of A,B,C
  3. for each of A,B,C push the branches to the teacher repo under a suitable name. E.g you might push A's master branch as A/master. The git push command can choose a different remote name, e.g. git push local-branch:name-in-remote.

Git-Submodule

Git can link a folder to another repository as a submodule. When the teacher repository is checked out, the submodules can be checked out as well. A submodule is not part of the teacher repository so the history is not copied, but the submodule refers to a specific commit in the linked repository, thus referencing the correct history.

Git-Subtree

Git can perform a special kind of merge where you merge another branch into a folder. This performs a copy but keeps any history intact.

  1. create the teacher repository
  2. for each of A,B,C use git-subtree to merge that repository's master branch into a suitable folder in the teacher repo. For example, you might include the A master branch into a folder called A/.

Git-subtree has a similar effect to creating a new repository and manually copying the files into that repository, but unlike a manual copy all the history will be kept intact. It is therefore better to view the subtree approach as a kind of merging technique.


Please ask your teacher which approach they would prefer. If in doubt the git-subtree approach is likely best and easiest to use because it includes the complete history of all repositories into a single branch of a single repository. However, note that this will not work as easily if your A,B,C repositories contain multiple branches.

Licensed under: CC-BY-SA with attribution
scroll top