Home > OS >  when to use git remote instead of clone?
when to use git remote instead of clone?

Time:08-16

I have read about the difference between remote and clone, eg: Difference between git remote add and git clone

But I'm wondering when in practice I should opt for git remote. (I'm working on a server with no internet; we use git bare as a github alternative.)

I assume if I create a bare repo (empty) and then clone it, this would produce the same result as creating a repo, and then creating a bare repo, and pointing the former to the latter using git remote. Therefore, if I'm setting up a project, git remote feels more clumsy than cloning.

I might use it, however, if a single programmer has already written some code (and others are joining after the project has started). Rather than copy the programs to a new repo (resulting from cloning a new bare repo), they could use git remote to push/pull to the new bare repo, and programmers joining the project can clone the bare repo.

Does this make sense? My goal is to do things in the cleanest and simplest way.

[Edited: punctuation.]

CodePudding user response:

I'm not really following the use case that you are describing, but in any case, the following two are roughly equivalent (the end result is the same):

  1. git clone path/or/url/to/your/repo.git
    
  2. cd repo
    git init
    git remote add origin path/or/url/to/your/repo.git
    git fetch
    

The former being definitely easier to remember and fewer key strokes.

CodePudding user response:

Another use case of git remote add is when you have multiple upstream for a single repository.

For example, a common use case when working with Github is to have a fork repository in your account, and have both original and your fork added as remotes in your local copy. This way you can push to your own remote fork and keep track of changes in the original remote repository at the same time in a single repository.

Another example of this can be using a USB memory stick as a backup remote repository. After cloning from a remote Github repository, you can create a bare repo in a USB folder and add this folder as another remote to the same working copy. Then, you can frequently push to USB as a backup when working offline, for example.

  • Related