Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "git remote add" exactly do?

I've recently been introduced to git in a project I've started working on, which encouraged me to start reading the online version of "Pro Git" by Scott Chacon.

There is a section in the book which briefly touches the subject of adding a "remote repository". Since Git is a distributed SCM, I understand that a common situation of using GIT would be to "clone" a remote repository on your local machine and work on your local copy. When you're done with your changes and committed all changes to your local copy, you then "push" your changes to the remote repository so others can view your changes too.

Which brings me to my question - from what I've understood, "git remote add" effectively "adds" a remote repository as a shorthand notation which we can use later. What I don't understand is this - since I clone by only specifying one repository URL, how can I add multiple repository urls under my local project's directory?

To be precise, suppose I check out the Apache commons-lang project using

git clone git://git.apache.org/commons-lang.git

This results in GIT copying over the whole project in my local directory named "commons-lang". Since I've used only one url for cloning, what does doing a "remote add" mean here? Does it mean that the same project (commons-lang) could be hosted on multiple servers, or is it that I could clone a new project within my local project by adding a new remote address?

Apologies for beginner-like tone of the question. Just trying to get my facts dead straight. Thanks.

like image 940
divesh premdeep Avatar asked Dec 09 '12 10:12

divesh premdeep


People also ask

What is the difference between git clone and git remote add origin?

git remote add just creates an entry in your git config that specifies a name for a particular URL. You must have an existing git repo to use this. git clone creates a new git repository by copying an existing one located at the URI you specify.

What is git remote add origin command?

1. git remote add. This command is used to add a new remote, you can use this command on the terminal, in the directory of your repository. The git remote add command takes two arguments: A remote name, for example, origin.

What's a git remote?

What is a Git Remote? A remote repository in Git, also called a remote, is a Git repository that's hosted on the Internet or another network. Watch this beginner Git tutorial video to learn how to Git clone a remote repository to create a local version of the repository on your machine.


1 Answers

You can add as many upstream repo address you want with git remote add (from "Git Basics - Working with Remotes").

When cloning, one is added for you, named 'origin'.

Does it mean that the same project (commons-lang) could be hosted on multiple servers,

Yes, that is due to the distributed nature of Git, since pushing and pulling introduces a publication workflow which is orthogonal to the merge workflow.

or is it that I could clone a new project within my local project by adding a new remote address?

No. You can fetch from different upstream repos.

See "Definition of “downstream” and “upstream”".

One classic scenario where you git remotes add is when forking on GitHub:
See "How do I merge locally a master and a fork in git?"

remote add

  • origin is added for you when cloning your fork on GitHub.
  • but you need to git remote add upstream https://github.com/user/reponame in order for your local clone to keep up-to-date with the original repo (hte one you cannot contribute directly, which is why you had to make a fork on GitHub in the first place).
like image 159
VonC Avatar answered Oct 26 '22 08:10

VonC