How To Git Clone, Push, and Pull Over SSH (2024)

The short answer

To clone a Git repository using the SSH protocol, you can use the [.inline-code]git clone[.inline-code] command with a valid SSH URL as follows:

 $ git clone git@host:username/repository.git

Where:

  • [.inline-code]host[.inline-code] represents the domain name or the IP address of the hosting server.
  • [.inline-code]username[.inline-code] represents your user account.
  • [.inline-code]repository[.inline-code] represents the name of the Git repository you want to clone.

For example:

 $ git clone git@github.com:johndoe/my-app.git

[#cloning]Cloning with SSH vs. HTTPS[#cloning]

The main difference between cloning a remote repository with SSH and HTTPS is the way the authentication is handled.

When using HTTPS, Git will prompt you for your username and password during the authentication process.

On the other hand, when using SSH, Git uses your SSH key to authenticate, which means that you don’t need to send your credentials over the network.

In that sense, SSH is a more secure method for cloning repositories and pushing / pulling commits, as only the machines with the key file on disk are able to access the repositories.

Moreover if the SSH key file was to be stolen, it won’t give access to the account itself (unlike the credentials) and can be easily revoked.

[#ssh-for-git]Set up SSH for Git[#ssh-for-git]

In order to be able to clone a remote Git repository using the SSH protocol, you will have to create a new SSH key pair on your local machine, and add this key to your Git hosting service. We will be using GitHub as our Git hosting service in the following examples, but others will work just as well.

[#recall-syntax]Using Warp's AI to quickly retrieve these steps[#recall-syntax]

If you’re using Warp as your terminal, you can easily retrieve an approximation of the steps described below using Warp's AI feature:

How To Git Clone, Push, and Pull Over SSH (1)

For that:

  1. Click on the bolt icon on the top right of the terminal
  2. Type in your question; for example: "How do I add a new SSH key to my Github account".
  3. Press [.inline-code]ENTER[.inline-code] to generate an answer.

As with any AI-powered tool, use it as a starting point and not a final answer. We’ll dig into more depth in our human-powered writeup below.

[#generate-key]Step 1: Generate a new SSH key with ssh-keygen[#generate-key]

To generate a new SSH key pair on your local machine, you can use the [.inline-code]ssh-keygen[.inline-code] command as follows:

 $ ssh-keygen -t ed25519 -C “user@example.com”

Where

  • The [.inline-code]-t[.inline-code] flag is used to specify the type of key to create, in this case, an ed25519, which is a popular type of public-key cryptography.
  • The [.inline-code]-C[.inline-code] flag is used to provide additional information about the key, such as its purpose or the user who generated it.

When prompted with this question, simply press [.inline-code]ENTER[.inline-code] to validate the recommended file path the key pair will be saved at, or type in another path if a similar file already exists in the [.inline-code].ssh[.inline-code] directory.

 Generating public/private ed25519 key pair. Enter file in which to save the key (/home/johndoe/.ssh/id_ed25519):

To the following questions, type in a passphrase to secure your key, and press [.inline-code]ENTER[.inline-code] once again to complete the process.

 Enter passphrase (empty for no passphrase): Enter same passphrase again:

You should now see a similar output in your terminal window confirming that the key was successfully generated:

 SHA256:ToTEp33dDV8Sokslnx568DC5ABPQTmvlBjGx+r/W0k8 user@example.com The key's randomart image is: +--[ED25519 256]--+ |o.+o .. | |. +o....+ | | +.= .o. | |o.+ o . . . | |o+ . . .So . | |*. oo+ o . | |o*. o =E= o | |ooo.o =..o o | |.o+o++ .. | +----[SHA256]-----+

And you should be able to find the following files in your [.inline-code].ssh[.inline-code] directory:

 $ ls ~/.ssh id_ed25519 id_ed25519.pub

Where:

  • The [.inline-code]id_ed25519[.inline-code] file is your private key whose content should be kept confidential.
  • The [.inline-code]id_ed25519.pub[.inline-code] file is your public key that we'll use in the next step.

[#add-public-key]Step 2: Add a public key to your GitHub account[#add-public-key]

To add a public key to your GitHub account, first display the content of the public key file you've just created using the [.inline-code]cat[.inline-code] command, and copy it to your clipboard using [.inline-code]CTRL+C[.inline-code] (or [.inline-code]CMD+C[.inline-code] on MacOS).

 $ cat ~/.ssh/id_ed25519.pub

Alternatively, you can use the [.inline-code]pbcopy[.inline-code] command on MacOS as follows:

 $ pbcopy < ~/.ssh/id_ed25519.pub

Once you've done that:

  1. Log in to your GitHub account.
  2. Navigate to “Settings”.
  3. Click on “SSH and GPG keys” in the left menu
  4. Click on the “New SSH key” button.

Or directly follow this link https://github.com/settings/ssh/new.

From here:

  1. Add a short descriptive title in the [.inline-code]Title[.inline-code] field.
  2. Paste the public key in the [.inline-code]Key[.inline-code] field
  3. Click on the "Add SSH key" button to finalize the process.

How To Git Clone, Push, and Pull Over SSH (2)

[#clone]Step 3: Clone a repository[#clone]

To verify that the SSH key you’ve just added to your account works:

  1. Navigate to the repository you wish to clone.
  2. Click on the "Code" button.
  3. Copy the URL located under the "SSH" tab.

How To Git Clone, Push, and Pull Over SSH (3)

  1. Run the [.inline-code]git clone[.inline-code] command in your terminal with the URL you just copied.

[#specify-key]Step 4: Specify the SSH key to the ssh-agent[#specify-key]

If you don't want to type in your password every time your SSH key is used by Git, you can add your key to the list of keys managed by the SSH agent. To do so, first make sure that the SSH agent is running using the following command:

 $ eval "$(ssh-agent -s)"

Then add your private key file using the following [.inline-code]ssh-add[.inline-code] command:

 $ ssh-add ~/.ssh/id_ed25519

[#commits-with-ssh][.inline-code]git[.inline-code] pushing and pulling commits with SSH[#commits-with-ssh]

Once you've cloned a Git repository on your local machine with SSH, every commit you push and pull will automatically go through the SSH protocol, with no additional configuration steps required.

How To Git Clone, Push, and Pull Over SSH (2024)

FAQs

How to clone a Git repository over SSH? ›

How to Clone a GitHub Repository Using SSH on a Linux Machine?
  1. Step 1: Generate SSH Key. Generate a new SSH key using the ED25519 algorithm, associating it with your email. ...
  2. Step 2: Install xclip. ...
  3. Step 3: Copy SSH Key to Clipboard. ...
  4. Step 4: Add SSH Key to Git Repository. ...
  5. Step 5: Clone the Repository.
Apr 8, 2024

How to Git push with a specific SSH key? ›

SSH keypair setup for GitHub (or GitHub/GitLab/BitBucket, etc, etc)
  1. Create a repo. Make sure there is at least one file in it (even just the README.md)
  2. Generate a SSH key pair (private/public): ...
  3. Copy the contents of the public SSH key. ...
  4. Copy the public SSH key to GitHub. ...
  5. Test the SSH key:

How to clone repo with specific SSH key? ›

To clone a project from GitHub using an SSH key, follow these steps:
  1. Create an SSH key on your local machine if you don't have one already. Open a terminal and enter the following command: ...
  2. Add the SSH key to your GitHub account. ...
  3. Ensure your Git client is set up to use SSH. ...
  4. Clone the GitHub repository using the SSH URL.

Should I clone with https or SSH? ›

Use SSH as a more secure option and HTTPS for basic, password-based Git usage. Since SSH is more secure than entering credentials over HTTPS, it is recommended for businesses dealing with sensitive and critical data. Once you generate the SSH keys, only the machines with the key file on disk can access the repository.

How to use git via SSH? ›

Using Git via Command Line
  1. Step 1: Get SSH Access. Connect to your server via SSH. ...
  2. Step 2: Generate SSH Key (Master Credentials Only) ...
  3. Step 3: Upload the SSH Public Key to Your Git Repository. ...
  4. Step 4: Deploy Code Using Git Commands.

How to clone a project with a SSH key in GitLab? ›

In GitLab, navigate to the repository you wish to clone, and copy the SSH URL. Switch back the the “Variables” tab in your Sophos Factory pipeline and add the additional variables as shown in the image below. Paste the SSH URL from GitLab into the value for the Repository URL. Make sure to save when you are done!

Is a SSH key required for git clone? ›

In some cases, you might need to use a specific SSH key when cloning a Git repository. This could be due to having multiple keys for different accounts or repositories, or because the key you need to use is not in the default location. Here's how you can specify a particular SSH key when running the git clone command.

How do I manually clone a repo? ›

Clone a repository with VS Code
  1. From the repository, select the Clone button.
  2. In the Clone this repository dialog, select the Clone in VS Code button. ...
  3. In VS Code, select Clone a new copy from the dropdown menu.
  4. When prompted, select the local storage location where you want to keep the cloned repository.

How to clone without SSH key? ›

Ease of setup: Cloning with HTTPS is straightforward and doesn't require setting up SSH keys. Universally accessible: HTTPS cloning works through firewalls and proxy servers.

How to check if git is using SSH or HTTPS? ›

You can check to see how your project is connected to git by running a git connection test from the git action panel to see your remote repository URL. If this URL starts with “https://github” it is configured using https, if it starts with git@ it is configured using ssh.

How to use git with SSH instead of HTTPS? ›

git with ssh instead of https
  1. generate an ssh keypair on your linux box. ssh-keygen -t {rsa|dsa}
  2. add the public key to github: profile - settings - ssh keys.
  3. switch from https to ssh.

How do I change cloned repo from HTTPS to SSH? ›

Switching remote URLs from HTTPS to SSH
  1. Open Terminal .
  2. Change the current working directory to your local project.
  3. Change your remote's URL from HTTPS to SSH with the git remote set-url command. git remote set-url origin git@github.com:OWNER/REPOSITORY.git.
  4. Verify that the remote URL has changed.

How to clone a Git repository in terminal? ›

Clone a repository using the command line
  1. From the repository, select the Clone button.
  2. Copy the clone command (either the SSH format or the HTTPS). ...
  3. From a terminal window, change to the local directory where you want to clone your repository.
  4. Paste the command you copied from Bitbucket, for example:

How do I clone my own Git repository? ›

To Git clone a repository navigate to your preferred repository hosting service like GitHub, select the repository you want to clone, copy the repository URL via HTTPS or SSH, type git clone in the command line, paste the URL, and hit enter .

How to clone Git repository in Visual Studio using SSH key? ›

Under setting select SSH and GPG keys, select ssh and new ssh key. Give the key a name, in the box below key paste in the public key you copied from the step above. click add ssh key. Go to the repo you wish to clone and select code, copy the ssh link and return to the terminal in vscode.

How to create a ssh key for Git? ›

Generating a new SSH key
  1. Open Terminal .
  2. Paste the text below, replacing the email used in the example with your GitHub email address. ssh-keygen -t ed25519 -C "your_email@example.com" ...
  3. At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases."

Top Articles
Everything You Need to Know About File Encryption
Better Safe Than Sorry: Which Files Do You Need to Encrypt?
Navicent Human Resources Phone Number
Radikale Landküche am Landgut Schönwalde
Worcester Weather Underground
How To Do A Springboard Attack In Wwe 2K22
Dew Acuity
Rek Funerals
Mylaheychart Login
Mail Healthcare Uiowa
Kostenlose Games: Die besten Free to play Spiele 2024 - Update mit einem legendären Shooter
Carter Joseph Hopf
Locate Td Bank Near Me
Valentina Gonzalez Leaked Videos And Images - EroThots
DIN 41612 - FCI - PDF Catalogs | Technical Documentation
The most iconic acting lineages in cinema history
Luna Lola: The Moon Wolf book by Park Kara
Walmart End Table Lamps
Teenleaks Discord
Walmart stores in 6 states no longer provide single-use bags at checkout: Which states are next?
Where to Find Scavs in Customs in Escape from Tarkov
10 Fun Things to Do in Elk Grove, CA | Explore Elk Grove
The Weather Channel Local Weather Forecast
Jeffers Funeral Home Obituaries Greeneville Tennessee
Zillow Group Stock Price | ZG Stock Quote, News, and History | Markets Insider
Conscious Cloud Dispensary Photos
Criterion Dryer Review
Random Bibleizer
Costco Jobs San Diego
Wolfwalkers 123Movies
Mosley Lane Candles
Willys Pickup For Sale Craigslist
Club Keno Drawings
Star News Mugshots
Ravens 24X7 Forum
Cars And Trucks Facebook
Tamilrockers Movies 2023 Download
Garrison Blacksmith's Bench
Daily Jail Count - Harrison County Sheriff's Office - Mississippi
Why Holly Gibney Is One of TV's Best Protagonists
Skill Boss Guru
Heelyqutii
What Is Kik and Why Do Teenagers Love It?
Ig Weekend Dow
Bekah Birdsall Measurements
Po Box 101584 Nashville Tn
20 Mr. Miyagi Inspirational Quotes For Wisdom
Accident On 40 East Today
Jackerman Mothers Warmth Part 3
Oefenpakket & Hoorcolleges Diagnostiek | WorldSupporter
Qvc Com Blogs
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 6487

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.