Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set git remote url to a sub folder in the repository?

Tags:

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

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

Can I set a remote url for just project1 ?

like image 708
Aditya Avatar asked May 21 '18 15:05

Aditya


People also ask

How do I change the URL of a remote git repository?

In order to change the URL of a Git remote, you have to use the “git remote set-url” command and specify the name of the remote as well as the new remote URL to be changed.

How do I add a URL to a repository?

To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A unique remote name, for example, “my_awesome_new_remote_repo” A remote URL, which you can find on the Source sub-tab of your Git repo.


2 Answers

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.

like image 143
torek Avatar answered Sep 28 '22 17:09

torek


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.

like image 34
HieroB Avatar answered Sep 28 '22 16:09

HieroB