Using ssh to clone a Private hit repository (2024)

Using ssh to clone a Private hit repository (2)

* prerequisite: GitHub account,Vscode, and Git CLI

Https is a common and secure way to clone a Git repository. One of it’s drawbacks, it may require your username and password in the url. There are ways around it, so let’s talk about using SSH.

SSH to clone a Git repo is a better option. When it comes to best practices. SSH or (secure shell) provides a secure connection from sever to your local machine using key pairs. The keypair consists of a public and a private key. The public key will be stored to your Git repo while the private key is save to a file on your local machine. This will allow the Git repo and your local machine to communicate using ssh. This limits repo access to the machine that generates the key pair, and to whoever is issued a copy of the private key.

Let’s “Git” started.

First you want to create a new repository. Login into you GitHub Account, select new repository and make it private. Give the repo a name, add .gitignore and README.md, create repository.

*Note: when adding .gitignore select the correct language for the repo, this will insure certain files will not be tracked by GitHub and will not be seen in the repo.

Using ssh to clone a Private hit repository (3)

Open up vscode , select the view tab and open a new terminal. In the terminal switch to the Git Bash shell. Using the CLI we need to create a new directory to store the cloned repo. Change into the new directory. Follow the commands and example below.

###create new directory
mkdir <name-of-directory>
###change directory
cd <name-of-directory>
Using ssh to clone a Private hit repository (4)

To clone the repo via ssh we will need to create a keypair. ssh_keygen command will generate a public/private rsa key. Using the CLI enter the command below.

ssh-keygen -t rsa -b 4096

After the key pair is generated you will be asked to enter a file to save the key. Note that it creates a default path to where the key is saved if left unchanged. I used the default path for simplicity. Press Enter 3x

Using ssh to clone a Private hit repository (5)

*Note: For this example we skipped passphrase, but adding passphrase is a best practice as it adds security to your ssh keys.

Now cd into the directory where the key is saved (in this case .ssh) and use the ls command to list the files in the directory. You will see the id_rsa and id_rsa.pub, Using the cat command open the the public key, and copy the text. This will be used in your GitHub account in the next step. Follow the commands and example below.

###list file in directory
ls
###open public key file
cat id_rsa.pub
Using ssh to clone a Private hit repository (6)

Back in GitHub click your profile picture in the top right hand corner, select settings. 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.

Using ssh to clone a Private hit repository (7)

Go to the repo you wish to clone and select code, copy the ssh link and return to the terminal in vscode.

Using ssh to clone a Private hit repository (8)

Using the cd command change to the directory you created to save the cloned repo. Type git clone and paste the link copied from GitHub. When prompted to type yes to continue. Follow the command and example below the repo is now cloned.

git clone <ssh-link-from-GitHub>
Using ssh to clone a Private hit repository (9)

Lastly go to file, open file, and select the file with the name of the cloned repo. All of the contents of the repo are ready to use in vscode.

Using ssh to clone a Private hit repository (10)

Congats!!! You have cloned your private GitHub repo using ssh and vscode. I hope you find this helpful. Check out my other medium post for more tutorials in tech.

Thanks for your time and attention.

Using ssh to clone a Private hit repository (2024)

FAQs

How do I clone a private GitHub repository using SSH? ›

How to Set Up SSH and Clone Repository Using SSH in Git
  1. Set up SSH. Step 1: Generate SSH Key Pair. Step 2: Add Key To GitHub. Step 3: Test the Connection. Step 4: Check Local SSH Agent. ...
  2. Clone Repository Using SSH Protocol. Step 1: Go to Repository Page. Step 2: Obtain SSH URL. Step 3: Clone Repository.
  3. Cloning with SSH vs. HTTPS.
Nov 2, 2023

How to clone a git repository using SSH? ›

To clone a Git repository using the SSH protocol, you can use the git clone command with a valid SSH URL as follows:
  1. $ git clone git@host:username/repository.git.
  2. $ git clone git@github.com:johndoe/my-app.git. ...
  3. $ ssh-keygen -t ed25519 -C “user@example.com” ...
  4. Generating public/private ed25519 key pair.

Is it better to 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.

Why can't I clone a private repo? ›

If you are trying to clone a private repository but do not have permission to view the repository, you will receive this error. Make sure that you have access to the repository in one of these ways: The owner of the repository. A collaborator on the repository.

How to clone a private repo without a password? ›

Using ssh key allows you to work with private repos easily without username password authentication. Github now has an option for PAT (personal access token, read about it here), but I still use ssh key option. Mac/Linux: git should be pre-installed.

How to clone a private git repository in VS Code? ›

To clone a repository, run the Git: Clone command in the Command Palette (Ctrl+Shift+P), or select the Clone Repository button in the Source Control view. If you clone from GitHub, VS Code prompts you to authenticate with GitHub. Then, select a repository from the list to clone to your machine.

Is SSH key required for git clone? ›

Git is capable of using SSH keys instead of traditional password authentication when pushing or pulling to remote repositories. Modern hosted git solutions like Bitbucket support SSH key authentication.

How to use git SSH? ›

Steps to setup GitHub SSH connections
  1. Create a GitHub SSH key pair with the ssh-keygen command.
  2. Copy the value of the public SSH key to the clipboard.
  3. Login to GitHub and navigate to your account settings.
  4. Click on the link for SSH and GPG keys.
  5. Click Add Key to register the public SSH key with your account.
Jan 30, 2024

How to clone a git repository in terminal? ›

Cloning a repository using the command line
  1. Open “Git Bash” and change the current working directory to the location where you want the cloned directory.
  2. Type “git clone” in the terminal, paste the URL you copied earlier, and press “enter” to create your local clone.

Is SSH better for Git? ›

SSH can compress and transfer data more efficiently than HTTPS, which can improve the speed and bandwidth of your git operations. In addition, SSH can use stronger encryption algorithms and key lengths than HTTPS, reducing the risk of interception or compromise of your data.

Why use SSH with GitHub? ›

With SSH keys, you can connect to GitHub without supplying your username and personal access token at each visit. You can also use an SSH key to sign commits.

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

How do I know if I'm 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 clone a private branch in Git? ›

Command to clone a specific Git branch
  1. The –single-branch (two dashes) flag limits the clone to only one branch.
  2. The –branch (two dashes) flag specifies which specific Git branch to clone.
Sep 2, 2023

How to clone a private Git repository with token? ›

Click on the “Generate New Token” or “Create Token” button. Provide a meaningful name to identify its purpose and select the required permissions based on your intended usage. For cloning repositories, the “repo” or “read” permissions are typically sufficient. Click on the “Generate Token” button to generate your PAT.

Can people see if you clone a repo? ›

Conclusion: Yes, the owner of a repository will see when someone makes a fork on GitHub, but no, they will not see it when someone makes a clone somewhere else.

How to add ssh key to GitHub private repo? ›

To add an SSH key to your GitHub account, use the ssh-key add subcommand, specifying your public key. For authentication keys, if you're prompted to request additional scopes, follow the instructions in the command line. To include a title for the new key, use the -t or --title flag.

How to clone a private GitHub repository with personal access token? ›

1. Using HTTPS to Clone GitHub Private Repository
  1. 1.1. Create a Personal Token. a. Go to your GitHub account settings. ...
  2. 1.2. Clone the Private Repo. Supply the personal access token you just created as if it is the password. git clone https://<your_username>:<your_personal_access_token>@github.com/owner/repo_name.git.
May 6, 2023

How do I access a private GitHub repo? ›

Accessing Your Private GitHub Repository from the Cluster. Once the SSH key has been added to your GitHub account, you can access your private repository using the repository's SSH address. In your web browser and while logged in to your GitHub account, go to webpage for the private repository.

How to clone a private branch in git? ›

Command to clone a specific Git branch
  1. The –single-branch (two dashes) flag limits the clone to only one branch.
  2. The –branch (two dashes) flag specifies which specific Git branch to clone.
Sep 2, 2023

Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 5280

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.