config file
Create a config
file in your ~/.ssh
folder that will manage your accounts.
Host personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
Host work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
When cloning a new repo then you only have to specify hostname to your Git account. Example:
git clone git@personal:yusbk/nama_rep.git
git clone git@work:ybkamaleri/nama_rep.git
When your repo has submodule
to be able to push changes to remote, I have to change this url
url = https://github.com/yusbk/yusbk.github.io.git
to a new url with reference to SSH-key
url = git@personal:yusbk/yusbk.github.io.git
in the config files in these folder:
../cloned_repo/.git
../cloned_repo/.git/modules/module_repo
gitconfig file
This is another setting when using multiple git accounts without needing to change hostname. After
cloning repo inside a folder eg. ~/Git.work/
then you can edit config file called ~/.gitconfig
in
HOME dir.
[user]
name = Your Name
email = your@email.com
[includeIf "gitdir:~/Git-work/"]
path = .gitconfig-work
Then create a file named .gitconfig-work
with
[user]
name = Your Name
email = your@email.com
[core]
sshCommand = ssh -i ~/.ssh/id_work
Here -i
is identifying file. This setting makes all repositories under ~/Git-work
folder to use work
credentials automatically without needing to change hostname when cloning a repo.