Clone private VCS repository with composer

git

Sometimes, your Composer configuration uses a private repository to obtain a package.This usually looks like this in your composer.json file:

{
    ...
    "packages": {
        ...
        "vendor/package-name": "1.0.0"
        ...
    }
    ...
    "repositories": [
        {
            "type": "vcs",
            "url": "git@git.example.com:VCSvendor/VCSpackage-name.git"
        }
    ]
}

The above example uses the git protocol to download from the git repository. However, what happens if we want to deploy to GitLab, where we only have credentials set up for https://.. ? Or the code previously worked on a Linux machine and we want to use it in windows, where the git protocol is not configured ? It would be terrible to have to change the file each time we need to do an install or update..

I was recently introduced to this problem and I found out something that solves the problem easily. I’m posting it here, so it helps me in the future as well.

Introducing: “.insteadOf“:

git config --global url."https://git.example.com/".insteadOf "git@git.example.com:"

The above command will tell Git to always substitute “git@git.example.com” with “https://git.example.com/”, when it encounters it. This means that when you run “composer install” or “composer update”, the URL will be switched and git will be able to download what it needs successfully.
Of course, you can remove the “–global” option and do the change on a per-project basis.

The “insteadOf” configuration saves a lot of time when trying to replace any part of a url and is a cool feature to know.