Question

How do I have my remote repository have information of multiple mirror repositories?

I created a local repository and added an origin and server1

# git remote add origin https://github.com/user/repo.git
# git remote add server1 git@git.ttu.edu:bharath/repo.git

Now, I push the repository to origin and clone it on another machine. The cloned repository has no idea of server1. Is there a way that I can make server1 information available in the cloned repository? How do I do it?

Was it helpful?

Solution

Unfortunately, no. Git doesn't transfer the configuration over the wire because it can introduce security issues.

I think the best way to approach this is to make a script that adds the extra configuration that you want, and you have to remember to execute it. Document the need to run it as part of your set up process to make it easier to remember.

Sorry!

UPDATE:

Unfortunately, Git will not transfer hook scripts either. Again, this is a security precaution. You may be cloning a code base you don't trust, and it would be wrong-headed for Git to execute arbitrary code.

I realize this not what you want to hear, but it's the current state of the world. Something you could do, and I've done this is well, is to create an alias that helps you with this process:

[alias]
setup = !sh -c 'git remote add origin https://github.com/user/$1.git; git remote add server1 git@git.ttu.edu:bharath/$1.git' -

Then a git setup REPO_NAME would set up both remotes for you.

If you're keen on having a script executed for you, you could setup a post-checkout hook that checks for a script in a known location in the repository and execute it, if the script hasn't been executed already. post-checkout could be called quite often, so you may not want it to execute fully all the time. Then, you could setup a custom template for post-checkout in /usr/share/git-core/templates/hooks.

For example, you could setup /usr/share/git-core/templates/hooks/post-checkout to be:

#!/bin/sh
test -f "$GIT_DIR/.ran-setup" && exit 0

# Call the setup script in the repo.
git show master:setup.sh > "$GIT_DIR/.setup-script" || {
    # No setup.sh exists at the top of the repo, so there's nothing to execute.
    exit 0
}

chmod +x "$GIT_DIR/.setup-script"
"$GIT_DIR/.setup-script" || {
    echo 1>&2 "error: setup script failed to execute correctly."

    # Clean up.
    rm -f "$GIT_DIR/.setup-script"

    # Note: this doesn't prevent anything from happening in Git.  Git doesn't
    # care about the exit value of the post-checkout script.
    exit 1
}

# Clean up.
rm -f "$GIT_DIR/.setup-script"

touch "$GIT_DIR/.ran-setup"

This will check for a setup.sh at the top-level of the master branch and attempt to execute it the first time it sees it. Then in your setup.sh script, you could do:

#!/bin/sh

# Attempt to get the repository name from the origin.
repo_name=$(git config remote.origin.url |
            sed -e 's|.*/\([^/]*\)$|\1|' |
            sed -e 's|\.git$||')

# Add the remote as long as there's a repo name.
if [ "$repo_name" != '' ]; then
    git remote add server1 "git@git.ttu.edu:bharath/${repo_name}.git"
fi

You can put the templates in an alternate location, if you don't have the ability to modify the system-wide ones or don't want to (because the system is shared with other users who may not want this feature, for example). You can either use the --template option to git clone to point to the alternate location, or set the following in your ~/.gitconfig:

[init]
        templatedir=/path/to/new/template/dir

The downside of any approach like this is that a person could get you to execute arbitrary code. So a "friend" could convince you to clone a repository that would end up erasing your hard drive, or something worse. And, that's why Git doesn't support this concept natively.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top