Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 3

How to use git with multiple remote repositories?

Solved! Go to Solution.

   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:

git clone git@github.com:typicaljoe/taffydb.git taffydb

Now let’s enter that directory and have a look around:

cd taffydb
git remote -v
  # origin  git@github.com:typicaljoe/taffydb.git (fetch)
  # origin  git@github.com:typicaljoe/taffydb.git (push)

Let’s add our upstream branch to ltullman.

# create new branch
git checkout -b ltullman-master 

# add remote repository
git remote add ltullman git@github.com:ltullman/taffydb.git

# fetch the master branch contents
git fetch ltullman master

# and now set upstream
git branch --set-upstream-to=ltullman/master
2 Replies
Solution
Commander Lvl 1
Message 2 of 3

Re: How to use git with multiple remote repositories?

Hi @JohnBartlesAccount ,

 

 Welcome to the GitHub Community forum!

 


From the line below which reads "git checkout -b ltullman-master", what branch is the newly created  branch "ltullman-master" based on?

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:

git push <remote> <new.branch>

Where remote is the remote where you want this branch.

 

 


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"?


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.

 

 

...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.


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

                                        \

                                         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:

1) if the itullman remote have the same history

                   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.

2) if the itullman remote have the same history

                   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.

3) if the itullman remote have the same history

                   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-

 

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!
Copilot Lvl 2
Message 3 of 3

Re: How to use git with multiple remote repositories?

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.