Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

Another way to use different SSH keys for different git repos

git truly does it all

Joel Clermont
Joel Clermont
2025-07-29

From our daily Laravel tips email. Get the next one in your inbox.

I previously shared a tip on using different SSH keys per git repo.

This led to some feedback from Hammed Oyedele with a related situation that requires a different solution.

What if the repo needing a specific SSH key is a privately-hosted package used in your project's composer.json file?

You can't as easily manipulate the URL to match your local SSH config since it would require everyone on the team to agree to the same SSH alias, as well as your CI and other environments. It might be possible, but it's certainly not ideal.

Instead, we need to add one more step to our previous tip, and update our global git config, typically found at ~/.gitconfig:

[url "bitbucket-work-alias:work-organization"]
    insteadOf = [email protected]:work-organization

With this in place, any git commands we run, including that Composer install, that have a URL starting with [email protected]:work-organization will automatically be rewritten with our local SSH alias bitbucket-work-alias.

It's important to include the work-organization to limit this replacement logic to just that specific user or organization.

If we had more than one org or user to match, we could add additional sections to our global git config to cover them all.

Now we can seamlessly use our local SSH alias without requiring the rest of our team to change their local configuration.

Here to help,

Joel

P.S. Would you like help getting your project off an older PHP or Laravel version, but without breaking anything?

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.