Using multiple git profiles
Issue
I am using a laptop where I have my personal repositories and some work ones. I would like to commit to these using different profiles and secure them using different SSH keys.
Assumptions
I will assume you are:
- using a linux system.
- Have generated different SSH keys for each profile:
~/.ssh/personal-key
~/.ssh/work-key
- Your repositories are on GitHub, though you can change the link from GitHub (
github.com
) to another repository host.
Solution
\1. Add hosts to your ~/.ssh/config
file such as:
Host work-github
HostName github.com
User git
IdentityFile ~/.ssh/work-key
IdentitiesOnly yes
Host personal-github
HostName github.com
User git
IdentityFile ~/.ssh/personal-key
IdentitiesOnly yes
For later steps, “work” and “personal” will be referred to as <account>
.
2a. Change the remote host for an existing repository:
cd /path/to/your/repo
git remote set-url origin <account>-github:/remote/path/of/repo.git
2b. Clone a repository using the new host:
cd /path/to/the/folder/for/repositories
git clone <account>-github:/remote/path/of/repo.git
\3. Set the user details for the repository:
git config user.name "Your Name"
git config user.email "[email protected]"