Now let say that you were working in a company and have a different Github account to maintain their source code, either you have to pull, commit and push, then you want to manage your own source of code, for example, maybe some of your projects, etc.
Of course, changing credential or account every time you want to push code to a different account would be such a pain, and I have a solution for this.
Important
- This command that I'm running was using Windows OS
- Using Git Bash as my command terminal
- Visual Studio Code and Notepad++ as Editor
Steps
1. Open a folder of your repository location
2. Create a new SSH key and set user repository of Github
If you were using Windows, then use this command to generate a new SSH key
bash |
---|
ssh-keygen -t rsa -C “your_email_address” |
after that, it will show this,
bash |
---|
Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/your_username/.ssh/id_rsa): |
Copy-paste the file path and identify your key name,
bash |
---|
/c/Users/your_username/.ssh/id_rsa_KEY_NAME |
Then it will show this,
bash |
---|
Enter passphrase (empty for no passphrase): Enter same passphrase again: |
Just press "Enter" twice to leave it blank.
If you want to see all the SSH key that you already generate on your local machine, you can use this command,
bash |
---|
ls -al ~/.ssh |
You'll get something like this
bash |
---|
total 66 drwxr-xr-x 1 your_username 1049089 0 Jan 9 09:51 ./ drwxr-xr-x 1 your_username 1049089 0 Jan 9 09:38 ../ -rw-r--r-- 1 your_username 1049089 0 Jan 9 09:37 config -rw-r--r-- 1 your_username 1049089 1702 May 16 2018 id_rsa -rw-r--r-- 1 your_username 1049089 380 May 16 2018 id_rsa.pub -rw-r--r-- 1 your_username 1049089 1766 Jan 9 09:51 id_rsa_KEY_NAME -rw-r--r-- 1 your_username 1049089 408 Jan 9 09:51 id_rsa_KEY_NAME.pub -rw-r--r-- 1 your_username 1049089 11036 Dec 23 13:51 known_hosts |
id_rsa_KEY_Name is your private key,
id_rsa_KEY_Name.pub is your public key that you'll be provided to Github later on.
Copy the key inside id_rsa_KEY_Name.pub
could be something like,
bash |
---|
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzlQZ2QEc6tNzTAeF/fW6Wkw8K ----(because it's to long)---- |
Now let's go to Github!
Navigate to this GitHub account → Settings → SSH and GPG keys
Click on New SSH key
and now add the Title and the SSH key pub that you already open and copy-paste it to Key after you are done with the input, click Add SSH key.
now let's navigate back to your local machine folder repository (see step one) and add this following command
js |
---|
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_KEY_NAME" |
when you have done the command, then start to add your Github user and email like so,
js |
---|
git config user.name "git" git config user.email git@hub.com |
Comments
Post a Comment