5

For a repo (say "test"), that has the folder structure as

├── Project1
├── Project2
├── Project3

Can I set a remote url for just project1 ?

Aditya
  • 677
  • 2
  • 9
  • 15
  • 1
    Per-folder Git remotes don't make any sense. It looks like you're cramming multiple projects into a single repository. Git is designed to support one project per repository, and following that convention will simplify things for you. – ChrisGPT was on strike May 21 '18 at 15:15
  • 1
    This is doable if you can set up each of your projects as git submodules. See this answer for steps - https://stackoverflow.com/a/914090/866021 – Adil B May 21 '18 at 15:15
  • @Chris This projects are very small and I don't want to create repo for each one of them. Those are basically codes for all my college classes. – Aditya May 21 '18 at 15:20
  • @Aditya, then you'll have to deal with the implications of that. Single-project repositories are almost always the better choice (though of course there are exceptions like Google's famous monorepo). – ChrisGPT was on strike May 21 '18 at 15:21

4 Answers4

3

No and yes. That is, you can set this, but it will not do what you want, whatever that may be.

A remote is just a short name that acts as a key:

  • origin => remote.origin.url, remote.origin.fetch
  • xyz => remote.xyz.url, remote.xyz.fetch

and so on. You can set the url for each such key to anything you like: https://example.com/path/to/repo or /local/path or jellybeans or whatever you like, but the things that use this value are git fetch, git push, and git ls-remote. These all contact another Git repository at the given URL and transfer commits (not files) back and forth, or in the case of git ls-remote, tell you what commits and branch names are available.

Since git fetch and git push transfer entire commits, files come along with the commits for the ride, but they have the names you gave them in the commits.

You can, however, make commits that contain only files in Project1. Checking out such a commit will remove all the Project2 and Project3 files, and then checking out a commit that has all the files will bring back all the Project2 and Project3 files. But when you run git push or git fetch, you're likely to send and receive commits that contain all the files along with any commits that contain only the Project1 files, so that's probably not a great idea.

If you store each project in a separate Git repository, that will probably get you what you want. Although I generally recommend avoiding submodules, you can make each of these repositories a submodule of a single superproject that does nothing but coordinate all the submodules; this particular path is workable.

torek
  • 448,244
  • 59
  • 642
  • 775
1

In order to do a remote repository for your Project 1, I think you would need to start out by setting it up as a separate repository locally. As torek points out, in git the "remote" maps to a "repo"--you can manage your commits to include only updates to the Project 1 directory, but that's not really a separate remote.

If you set up the Project 1 directory as a separate git repository, you can add a remote to it. To get it into your directory tree, you can: a.) use symlinks in your main project tree to point to the Project 1 directory; b.) add it to your main project as a submodule.

You can get more info on submodules in the docs or in Mastering Git Submodules.

HieroB
  • 3,917
  • 4
  • 17
  • 22
0

Yes. You can. Use GitHub's submodules: https://blog.github.com/2016-02-01-working-with-submodules/

Vy Do
  • 46,709
  • 59
  • 215
  • 313
0

If you want to clone a subdirectory, this StackOVerflow question / answer says "no".

If you are looking for commiting in a subdirectory, then the answer is yes.

  1. Make changes to the subdirectory

  2. Then execute the following command to see which changes have been done:

    git status

  3. You will see the changes you made, for example:

    modified: application/Email.php modified: application/Config.php

  4. to add the changes, do this at the command line:

    git add application/*

  5. Commit your changes as follows:

    git commit -m "Add email suport to application"

  6. When you work on GitHub, GitLab or a similar platform, push your changes to the remote repository:

    git push origin master

For the last step, I assume that you have configured Git well and that you know how things work. Otherwise, use this tutorial to get started.

Jordy Deweer
  • 311
  • 1
  • 4
  • 13