0

I have a folder called coursework on my machine with many files like: file 1, file 2, file 3, file 4, file 5, and file 6. On Github, I want to create repo1 with ONLY file 1, file 2 and file 3. Also, I want to create repo 2 with ONLY files file 4, file 5 and file 6 from that same coursework folder.

Someone recommended me to use submodules but I am not entirely sure how to use them in this case. Is there an easier approach to accomplish this task? Thanks!

mike
  • 1,233
  • 1
  • 15
  • 36
Harman
  • 158
  • 1
  • 9
  • 1
    An individual git repo is effectively bounded by its containing folder. It would be so much simpler if you could just put file1, file2, and file3 into one folder and file4, file5, and file6 into another folder. Can you really really really not do that? Because if you could, you could just have two repos, one for each folder. – matt Jun 20 '20 at 17:06
  • Thanks for the quick response! I could certainly do that, in fact that's what I am doing for some of my other work. But my coursework folder has quite a lot of files and creating individual folders is quite a hassle. So I thought maybe there could be a workaround through git. – Harman Jun 20 '20 at 18:00
  • Well we're only talking about two folders, because two repos. So I don't quite get what the "hassle" is. – matt Jun 20 '20 at 18:49
  • I would be inclined to listen carefully to matts advice, working with submodules can be complex, and the complexity scales up when managing multiple submodules, have you read https://git-scm.com/book/en/v2/Git-Tools-Submodules ? – mike Jun 20 '20 at 19:21
  • No, @matt there will be more than just 2 folders. I am guessing 15+ at least. I gave that an example to simplify things. – Harman Jun 20 '20 at 20:01
  • Do you need them to **also** be available in one folder, like you have them now? If not, how about simply moving them to different folders and creating separate repositories? – Lasse V. Karlsen Jun 20 '20 at 20:07
  • @matt I very well know how to use my computer. Maybe you could've come up with a better answer(without making folders) if you had know more about git :) – Harman Jun 20 '20 at 20:13

1 Answers1

0

IF the folder option is not possible (one folder per repo), then you would need to define two repositories with the same worktree (same folder with all your files)

cd /path/to/common/folder
git --git-dir=../repo1.git init .
git --git-dir=../repo2.git init .

Then for each repo, you define your exclusion, to ignore the files of repo2 for repo1, and ignore repo1 files for repo2.

echo 'pattern1' > ../repo1.git/info/exclude
echo 'pattern2' > ../repo2.git/info/exclude

This is only possible if each set of file is easily referenced by one or a few patterns.
If you have to list each and every files you want to ignore, that won't scale well.

Finally, you need to define aliases:

alias git1='git --git-dir=/path/to/repo1.git'
alias git2='git --git-dir=/path/to/repo2.git'

Start by defining two different remote repositories:

cd /path/to/common/folder
git1 remote add origin https://github.com/<me>/repo1
git2 remote add origin https://github.com/<me>/repo2

And you can start adding, committing and pushing from the same folder.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @Sam Jatt, if you go with this answer, stay alert, as there is a source of problems when using submodules: keeping submodule references in superproject in sync with the submodules, and vice versa. – mike Jun 23 '20 at 12:09