Multiple git profile with different credentials!

Multiple git profile with different credentials!

Add multiple remotes with different credentials and location to you project!

Git is the most important software of a developers life. We use it to track changes in our code, check differences, log what and when and by whom it is changed and many more. We upload our code along with git history in remote repositories. GitHub, GitLab, BitBucket are some of the most popular git remote repositories where we can store our codes and projects.

What if we need to store same project into multiple remote repositories!

Well that would be a pain if we communicate over HTTPS with our remote. Easy solution would be communication over SSH communication.

Assuming we have setup different SSH public-private key pair for each remote, we will go to setting up different git profiles. If not, you can visit this blog to learn how to setup different SSH public-private key pair.

Some Basics

To fetch any remote files and folders, we write ssh commands like following snippet

user@host:repository_location

If we apply that to our real life scene, and fetch a demo repository I recently published on GitHub,

git@github.com:TheRakiburKhan/RKAPIService.git

Now let's move to the fun part

The Fun Begins

Let's assume we have created two SSH public-private key pairs naming "personal_key" and "work_key". We want personal to be on GitHub and work is also on GitHub but different profile.

Creating config file

We can create the file in two methods

  • Console
  • Any random text editor.

Creating the file in console

We have to navigate to the SSH root location first. To do that we can simply write the following command.

cd ~/.ssh/

Then we need to create the file, to do that, just write the following

touch config

Now we have created the file, we can open and edit it. (Don't worry, it the file was already there, you can still write above command!)

My favourite text editor is VisualStudio Code or VSCode. So I will open it using the following

code config

Don't worry, I know you like other editors too, so just put the name of editor instead of mine, meaning replace code with your own.

Creating the file in any text editor

Let's open our favourite code editor and create a file named "config" without any extension.

What should the file be!

Now let's write the following on the file,

Host github.com
    HostName github.com
    User git
    PreferredAuthentications publickey 
    IdentityFile ~/.ssh/personal_key

Host github.com-work
    HostName github.com
    User git
    PreferredAuthentications publickey 
    IdentityFile ~/.ssh/work_key

Understanding the what we wrote

Remember we learned on basic about SSH command?

user@host:repository_address

Now let's understand what we wrote in above code,

  • "Host" is the Alias of "HostName"
  • "HostName" is the domain of remote location
  • "User" is the name of SSH user
  • "PreferredAuthentications" is the secret key we want to use for authentication to remote location. "publickey" is stating that we user the public key of the "IdentityFile" key pair
  • "IdentityFile" is the file of public-private key pair.

Shifting the config file

The file named config we created in above section should be moved to SSH root folder of our respective Operating System.

Apply what we understood

We have learned so much now. Let's apply in real life. Shall we?

So the final output of the url for add remote will be

  • For personal profile
    git remote add origin git@github.com:<PersonalUserName>/<RepositoryLocation>.git
    
  • For work profile
    git remote add work git@github.com-work:<WorkUserName>/<RepositoryLocation>.git
    

Wrap Up

So here we are, learnt how to use different SSH key pair to setup different remote location and use that to setup SSH access. And after adding those remote we can simply push to our repository like this,

  • For pushing with personal profile
    git push origin main
    
  • For pushing with work profile
    git push work main
    

Thank you for reaching out to end. Hope we understood the discussion. For any confusion hit me on Twitter @TheRakiburKhan