Access git repository using SSH key in PyCharm on Windows and Mac machine

In this post, we are going to discuss how we can set up git bash, SSH keys, and PyCharam IDE to access a git repository using the command line on a Windows or Mac machine. First, we will set it up on a Windows machine followed by a Mac machine. The setup process is very similar except for a few differences.

Set up Git, SSH keys, PyCharm on a Windows machine

1. Install Git Bash

We need to download and install Git Bash to run git commands. It will enable us to use a number of UNIX commands along with git commands. The git bash can be downloaded from https://git-scm.com/downloads.

Download Git Bash
Download Git Bash

Once it is downloaded, we can double click on the executable in order to install it. We don’t need to modify any default options during the installation process.

2. Generate SSH keys

Next, we need to configure the SSH keys which will help us to execute git commands from Git Bash or from a terminal or using an IDE like PyCharm. We need to follow the below steps to generate the SSH key pair:

  • Open Git Bash
  • Execute the below command to make sure that we are in the home directory.
 
cd ~/
  • Now execute the below command. Here, we are using the RSA key algorithm of 4096 bits. Replace the email address with your email address before executing the below command.
ssh-keygen -t rsa -b 4096 -C "youremail@example.com"
  • This will prompt you to enter a filename to store the private and public keys.
  • Just press the Enter key to accept the default filename which is id_rsa and id_rsa.pub. The key pair will be generated at the location /Users/your name/.ssh/id_rsa.
  • Next, it will ask you to create a passphrase which is optional. We can create a passphrase or press enter for no passphrase. Here, we are not using any passphrase. To use no passphrase, just hit enter twice.

3. Upload SSH public key to your Git account

Now, we need to upload the SSH public key to GitHub:

  • Your public key is stored in the file ending with .pub, i.e. ~/.ssh/id_rsa.pub. Open this file in notepad and copy the values.
  • Once you have copied your public SSH key, log in to your GitHub account and go to https://github.com/settings/profile
Github profile settings
Github profile settings
  • On the left-hand side menu, you will see a link “SSH and GPG keys”. Click on this link.
  • It will take you to the page where you can add the public SSH key which you have copied already.
  • Click the button “New SSH key”.
Add new SSH key
  • Then enter a key title name. It can be anything, e.g. NewMac or MyWindowsMachine, and etc.
  • Paste the public SSH key in the key textbox.
  • Click “Add SSH key”. It will add this public key to your git account.
Added SSH key
Added SSH key

4. Install PyCharm

We can install the PyCharm Community edition from the below links. Please note that we are installing the community edition and not the professional edition which is the paid version of the PyCharm.

Download PyCharm

Once downloaded, double click on the executable to start the installation process and follow the installation wizard.

5. Clone the repository

Now, to clone the repository, follow the below steps:

  • Go to the repository page.
  • Click on the Code button at the right.
  • Click on the “SSH” tab.
  • Copy the SSH URL which looks like “git@github.com:SQLRelease/RepositoryName.git
Git clone command
Git clone command
  • Now, open the git bash and execute the below command.
git clone git@github.com:SQLRelease/RepositoryName.git

6. Open repository in the PyCharm IDE

To open the repository in PyCharm follow the below steps:

  • Open PyCharm.
  • Go to File -> Open.
  • Select the repository folder which we have cloned.

Now, let’s discuss how we can set up the git SSH on a Mac machine.

Set up Git, SSH keys, PyCharm on a Mac machine

Install brew

First, we need to install brew (https://brew.sh/)-

Open the terminal window and run the below command

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install Git Bash

To install the Git bash, we need to follow below steps:

  • Open the terminal window.
  • Run command
brew install git
  • Verify git version
git --version

Generate SSH keys

Same as we have done for a Windows machine,

Upload SSH public key to the Git account

Same as we have done for a Windows machine,

Install PyCharm

Same as we have done for a Windows machine, Just use the Mac link.

Clone the repository

Same as we have done for a Windows machine,

Fix warning ‘UNPROTECTED PRIVATE KEY FILE!’

In order to fix the Unprotected private key file warning, we need to execute the below commands:

sudo chmod 600 ~/.ssh/id_rsa
sudo chmod 600 ~/.ssh/id_rsa.pub

It will ask for your machine password. Enter the password and hit enter.

Open repository in the PyCharm IDE

Same as we have done for a Windows machine,

Thanks for the reading. Please share your inputs in the comment section.

Rate This
[Total: 3 Average: 2.3]

Leave a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.