Manage multiple git accounts

Hi! Do you have multiple git accounts or you just using one? I have multiple and everytime I reinstall my system I trying to find how I
configured it before… (i finally wrote this together here).

Im using multiple git config file rather than multipl…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by May Meow

Hi! Do you have multiple git accounts or you just using one? I have multiple and everytime I reinstall my system I trying to find how I
configured it before... (i finally wrote this together here).

Im using multiple git config file rather than multiple host names (i don't like to have somethink like git@github-work or git@github-personal).

How it's work?

First of all you need to create different folders for you personal and work projects (or more folders if you have more accounts). Into each folder
create new .gitconfig file which will looks like this one:

# ~/Work/.gitconfig.work
[user]
email = your@email
name = Your Name

[github]
user = "YourGithubName"

[core]
sshCommand = "ssh -i ~/.ssh/work.pub" # public key for your work profile

You will also need more SSH keys. I using 1Password so I copy only my public keys to the my .ssh folder.

SSH with 1Password

If you want to use 1Passowrd you need to configure ssh. Add following to your ~/.ssh/config file

Host *
    IdentityAgent ~/.1password/agent.sock

SSH With SSH-AGENT

If you want to use more traditional way you will need to configure SSH-AGENT

eval "$(ssh-agent -s)" && \
ssh-add -K ~/.ssh/<personal_key> 

Then edit your ~/.ssh/config

Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/<created_key>

OK now you have create your keys and gitconfig files for you profiles. Now you need to tell git to use proper config based on git repository location. Edit ~/gitconfig which will need to looks similar to this:

# ~/.gitconfig

[includeIf "gitdir:~/May/"] # include for all .git projects under May/ 
path = ~/May/.gitconfig.may

[includeIf "gitdir:~/Work/"] # include for all .git projects under Work/
path = ~/Work/.gitconfig.work

[core]
excludesfile = ~/.gitignore      # valid everywhere

You list all your configs with git config --list command.

That's all

Good! You can now clone repositories from multiple accounts based on in which folder you clonning it.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by May Meow


Print Share Comment Cite Upload Translate Updates
APA

May Meow | Sciencx (2022-10-24T19:45:32+00:00) Manage multiple git accounts. Retrieved from https://www.scien.cx/2022/10/24/manage-multiple-git-accounts/

MLA
" » Manage multiple git accounts." May Meow | Sciencx - Monday October 24, 2022, https://www.scien.cx/2022/10/24/manage-multiple-git-accounts/
HARVARD
May Meow | Sciencx Monday October 24, 2022 » Manage multiple git accounts., viewed ,<https://www.scien.cx/2022/10/24/manage-multiple-git-accounts/>
VANCOUVER
May Meow | Sciencx - » Manage multiple git accounts. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/24/manage-multiple-git-accounts/
CHICAGO
" » Manage multiple git accounts." May Meow | Sciencx - Accessed . https://www.scien.cx/2022/10/24/manage-multiple-git-accounts/
IEEE
" » Manage multiple git accounts." May Meow | Sciencx [Online]. Available: https://www.scien.cx/2022/10/24/manage-multiple-git-accounts/. [Accessed: ]
rf:citation
» Manage multiple git accounts | May Meow | Sciencx | https://www.scien.cx/2022/10/24/manage-multiple-git-accounts/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.