'Auto Replace URL with Mirror URL

Due to company policy we cannot access GitHub directly from company network, it has to be done via a company mirror. This is very painful when we need to run codes that needs to download dependencies from GitHub.

Right now I have to go into the code files and replace line by line for every URL from "github.com/username/repo_name" to "company_mirror.com/username/repo_name" where company_mirror.com is our company mirror.

Just wondering is there a way I can change the setting somewhere so that whenever something tries to access github.com it will access company_mirror.com instead? Could this be achieved by adding a few lines in bashrc?

System: Ubuntu 20.04



Solution 1:[1]

You should be able to add one or more insteadOf rules to your configuration:

url.<base>.insteadOf

Any URL that starts with this value will be rewritten to start, instead, with <base>. In cases where some site serves a large number of repositories, and serves them with multiple access methods, and some users need to use different access methods, this feature allows people to specify any of the equivalent URLs and have Git automatically rewrite the URL to the best alternative for the particular user, even for a never-before-seen repository on the site. When more than one insteadOf strings match a given URL, the longest match is used.

In this example, something like this should work:

# Rewrite HTTPS requests to GitHub.com
git config --global url."https://company_mirror.com/".insteadOf "https://github.com/"

# Rewrite SSH requests to GitHub.com
git config --global url."git@company_mirror.com:".insteadOf "[email protected]:"

And the end result will be something like this in your ~/.gitconfig:

[url "https://company_mirror.com/"]
        insteadOf = "https://github.com/"

[url "git@company_mirror.com:"]
        insteadOf = "[email protected]:"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris