How to Git Push With SSH Key

Abdul Jabbar Feb 02, 2024
  1. Generate SSH Key Pair
  2. Specify an SSH Key
How to Git Push With SSH Key

SSH stands for Secure Shell. It’s a key that provides us with an access certificate for SSH network protocol.

It provides access to a remote server between engines on an unassured open network. It is used in transferring data, files, and network management and gives access to remote servers from the origin.

When a developer wants to push, pull or duplicate files securely between local Git installation and remote Git repository, the first thing that a developer need is to create an SSH key pair on that server. Through this key, the developer will be identified by both and certified developer’s Git installation with the remote server.

Let’s see how we can generate an SSH key pair and then push the work in the modified state to the remote repository with this SSH key. Following are the details of generating the SSH key pair:

Generate SSH Key Pair

To create SSH key pair, we will go to Git Bash; after opening, we will use the following command with our email id:

ssh-keygen -t rsa -b 4096 -C "email@test.com"

Now, we have to enter the file’s location and passphrase as asked.

git ssh-keygen

As in the above section, you can see that based on the details input provided, the key is generated using the RSA method specified above. Once the key is generated, we have to use this key and push the necessary work to the remote repository.

Specify an SSH Key

Sometimes developers need another SSH key to push their work to a given Git repo. As ~/.ssh/id_rsa is a default server SSH deployment key that doesn’t allow the developer to push a local repo in the developer’s workspace to the Git server that may have a unique host.

For such scenarios, where the username and the hostname are identical, the developer should designate another SSH key and permission in their ~/.ssh/config. Let’s say the configuration seems as defined below.

Host git-as-anaa
  HostName git.com
  User git
  IdentityFile ~/home/key/xsshfile.thuc
  IdentitiesOnly yes

Host git-as-tomi
  HostName git.com
  User git
  IdentityFile /home/key/sshfile.ten
  IdentitiesOnly yes

If the configuration is like this, the developer will only use github-as-anaa and github-as-tomi, replacing the original hostname (git.anaa.com) in their URL.

git remote add anaa git@git-as-anaa:your-repo.git
git remote add tomi git@git-as-tomi:your-repo.git

The option IdentitiesOnly yes is used to stop the use of default identities. On the other hand, if we also have identity files identical to the default names, they will surely be preferred first to try because the option IdentityFile is not the same as other config options and gets attached to the lists of identities to try.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn