Easiest Way To Use And Manage Multiple Git Profiles On The Same Computer.

Hey people, Here’s another article that would make your programing/tech life a breeze 😅. I’m sure most of us have all encountered situations where we worked on totally different projects using the same Computer. Well, if you’ve never had to use a different Github/bitbucket e.t.c account or probably won’t, it wouldn’t hurt completing this read and getting the knowledge all to yourself.

Okay, let’s dive in.

let’s get started

We all know how we might get a new job and would be required to create a new GitHub profile with your new work email, technically we would have to set up git credentials and ssh keys to enable us to pull/push and perform other git actions. We are going to be using Github in this article.

First off, after creating an account on GitHub, we need to generate a new ssh key for this account.

ssh-keygen

After running ssh-keygen, we are asked to set a file where we want to save this ssh credential as seen above. Do not hit the enter button in a rush. Since we already have an existing ssh key saved on our PC and we do not want to override it. We are gonna write the path of the file we want to write ourselves.

For instance, we have /Users/<user name>/.ssh/id_rsa as the file suggested. We are going to to change the name of the file to a name of our choice. We are going to change this to id_rsa_bolathedev_medium_example. This sets the absolute path as

/Users/<user name>/.ssh/id_rsa_bolathedev_medium_example.

Follow the remaining instructions.

After successful creation of the ssh key, to see our newly generated ssh key, let’s print this to the terminal by running

cat ~/.ssh/id_rsa_bolathedev_medium_example.pub

The printed ssh key usually begins with ‘ssh-rsa’, ‘ecdsa-sha2-nistp256’, ‘ecdsa-sha2-nistp384’, ‘ecdsa-sha2-nistp521’, ‘ssh-ed25519’, ‘sk-ecdsa-sha2-nistp256@openssh.com’, or ‘sk-ssh-ed25519@openssh.com’.

Now, we are going to create a new ssh key in our new work account on github. Go to settings and click on ssh and gpg keys.

Then, click on New SSH key. Paste our generated SSH key in the key text area and give it a title of your choice, then save.

After this, create a folder for this new project via the GUI or terminal. Go back to your terminal and change directory to this newly created project directory.

In this directory, run

export GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_bolathedev_medium_example'

This command tells git which SSH key to use when pulling/cloning. To check if it is set correctly, running echo $GIT_SSH_COMMAND should print out ssh -i ~/.ssh/id_rsa_bolathedev_medium_example.

After all this, we can now clone our repository into this folder. This will allow you clone repositories from this new organization in different github account.

Note: we are not done yet, cloning and pulling works alright but pushing will still push with your code with your existing github account.

Moving on, after cloning this repository, cd into it and run

git config core.sshCommand 'ssh -i ~/.ssh/id_rsa_bolathedev_medium_example'

This sets this newly created SSH key as a local config in that folder.

Next, we set our local credentials.

git config user.name"<your preferred name>"

git config --add user.email"<your new email used on new github account>"

Note that we are not adding the — global flag as we do not want it to change everywhere. When you commit locally, run git log to make sure the credentials of your new github account show there. Pushing your commit shows your new github user as the author of this commit.

In summary, we are now using two different git profiles at the same time. Pushing code in your other projects still works with your default git profile. With this solution, you can work with as much as 5–100 git profiles on the same computer and you do not need to change credentials each time. This is a one time implementation and would work every time.

And that’s it, it’s a wrap.

Thanks for taking out your time to read another one of my articles. I promise to keep on delivering ❤️. Also, if you notice any wrong steps or practices done, You can always reach out and the article will be updated.

Much love from bolathedev.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store