gitgit-clonegit-mirror

How to avoid losing remote information when using git?


My objective is to simplify the process of updating my git mirror clones.

I have a git repository repo-A which is given by a supplier I have a git repository repo-B which is in my local git

I am able to clone repo-A add repo-B as a remote and push to repo-B. With this commands

git clone --mirror repo-A
git remote add  my-local-server repo-B
git push --mirror my-local-server

The result is that repo-B contains the same branches, commits, etc than repo-A. At this point I can fetch new code from repo-A and push it to repo-B

The problem is that if I clone repo-B in another computer. The configuration of the remotes is lost.

This means that if I do a git remote -v only the links to repo-B are present.

Is it possible to configure repo-B so that even after a fresh clone remote repositories links are present?

My objective is to be able to do

git clone repo-B
git fetch --all origin /* or the name of location repo-A is located */
git push --all my-local-server

I know that a similar behaviour can be achieved by

git clone --mirror repo-A
git push --mirror repo-B

Solution

  • Is it possible to configure repo-B so that even after a fresh clone remote repositories links [to repo-A] are present?

    No.

    Remember, git clone means:

    1. make a new, empty directory;
    2. run git init in the new empty directory;
    3. run git remote add in the new clone;
    4. run git config in the new clone if/as directed by options you supplied to git clone;
    5. run git fetch in the new clone; and
    6. run git checkout in the new clone.

    Only step 4 could do what you want—the git remote add in step 3 is so that step 5 will fetch from repo-B—and step 4 only obeys arguments you supply on the command line.

    You have two options:

    1. Don't use git clone, at least not directly. Write your own program that runs git clone, inspects the new clone, then runs git remote add as desired. Or,
    2. Use git clone via an alias or script that adds appropriate -c options so that step 4 sets up a remote. git remote add consists of running several git config operations: specifically, you must set remote.name.url and remote.name.fetch. So you can do these with the git clone command line.

    Note that both of these are quite similar: the real difference is whether you get repo A's URL from something retrieved from repo B, or hard-code it. If you choose to get the URL from something retrieved from repo B, remember that git clone copies all (reachable) commits, but no branches: step 6 is the one that creates a branch in the new clone. You would need to encode repo A's URL in some data fetched in step 5: probably some commit, perhaps found by a specially coded remote-tracking name (i.e., a branch name in repo-B that becomes a remote-tracking name in your clone), or in some data to be found via some tag name.