How to use git with multiple remote repositories? #23289
-
Hi I am trying to understand “git remote add”, and I am trying to understand it in the context of the below example marked “Getting set up”. I took the below example from the blog labeled, “Using git with multiple remote repositories”. From the line below which reads “git checkout -b ltullman-master”, what branch is the newly created branch "ltullman-master" based on ? From the line below which reads “git fetch ltullman master”, how can the current checkout branch “ltullman-master” fetch anything at all from the address “git@github.com:ltullman/taffydb.git” since “itullman-master” is originally based on the clone from the address “git@github.com:typicaljoe/taffydb.git”? …Wouldn’t there be a huge number of conflicts? In other words, it seems to me that the current checkout branch is fetching from one address but it’s originally based on and cloned from another address, and that seems like it shouldn’t work. =======Getting set up================ Below I illustrate checking out taffydb on the master branch, and then set up two alternate branches using two different source repositories. First, let’s get the canonical taffydb repository:
Now let’s enter that directory and have a look around:
Let’s add our upstream branch to ltullman.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @johnbartlesaccount , Welcome to the GitHub Community forum!
It is based on the commit where you are. Remember that git is a decentralized version controll sysyem so the repository in your local machine is different from the repository on remote. For example you clone a repository from remote where there are already some commits: A—B---C—D remote/master II (clone) A—B---C—D local/master You are now in the HEAD (commit D) of the master branch in your local repository. Now when you create a new branch it’s append something like this: D new.branch / A—B---C—D master But this is your local repository so the branch is not based in any of the remote repository. In your case new.branch is itullman-master. To merge this branch in one of the repository you need to do:
Where remote is the remote where you want this branch.
If I understood the question, the answer is: if there is a branch named master on Itullman it fetchs that on the new branch itullman-master.
You need to do the fetch to prevent the conflict in the master based on remote origin. In fact you create a the branch ltullman-master where now you try to do the fetch of the master branch of remote Itullman, this can generate a huge conflict or not. In the first case you “save” the master of your local repository because the fetch is made into the itullman-master. Take the example above, now you have this: A—B---C—D origin/master II (clone) A—B---C—D local/master </font> D local/itullman-master X—X---X—X itullman/master You are in the HEAD of local repository in itullman-master branch and you do a fetch, this is what happen:
A—B---C—D local/itullman-master II (fetch) A—B---C—D itullman/master Fetch result = Already up to date The history are the same.
A—B---C—D local/itullman-master II (fetch) Z—Y---X—W itullman/master Fetch result = Conflict This because the history of master of itullman remote repository are different from the itullman-master of local repository.
A—B---C—D local/itullman-master II (fetch) A—B---C—D---E—F itullman/master Fetch result = local/itullman-master are behind of 2 commits (Similar if local/itullman-master are ahead) If you haven’t understood something, ask for more. Hope this helps 🙂 -Gabriele-
|
Beta Was this translation helpful? Give feedback.
-
Thanks @wabri for the answer. You seemed to resolve my confusion. Your answer seems to imply that if a local branch has multiple upstream addresses then the branches on the upstream addresses, from which your local branch fetches, SHOULD have a fairly similar history OTHERWISE multiple upstream addresses(i.e. remotes) should not be used. I was just introduced to the idea of multiple upstream addresses a few days ago. From my limited perspective, ANYTHING could have been possible and anything could have been a reason for having multiple upstream addresses, and there could have been ANY number of right or wrong ways of managing and handling multiple upstream addresses. Looking back, however, I guess it was obvious that in order to use multiple remotes for a single local branch those remote branches must be, at least, fairly similar. Ok, buddy, thanks for the resolve. |
Beta Was this translation helpful? Give feedback.
-
The commands from the example didn’t quite work the way I wanted. Using the same repo names from the example, this is what did work (and makes more sense to me):
|
Beta Was this translation helpful? Give feedback.
Hi @johnbartlesaccount ,
Welcome to the GitHub Community forum!
It is based on the commit where you are. Remember that git is a decentralized version controll sysyem so the repository in your local machine is different from the repository on remote.
For example you clone a repository from remote where there are already some commits:
A—B---C—D remote/master
II (clone)
A—B---C—D local/master
You are now in the HEAD (commit D) of the master branch in your local repository. N…