Bootstrap FreeKB - Git (Version Control) - Clone a repository
Git (Version Control) - Clone a repository

Updated:   |  Git (Version Control) articles

The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.

 

The git clone and gh repo clone commands can be used to clone a git repository. Here is the syntax of the git clone command.

git clone <protocol>://<username>@<hostname or IP address of the Git system>:<SSH or HTTPS port><repository owner>/<repository name>

 

And here is the syntax of the gh repo clone command.

gh repo clone command <repository owner>/<repository name>

 

AVOID TROUBLE

If there is a firewall between the local PC and the origin Git repository, SSH or HTTPS (or both) protocols will need to be allowed to pass through any firewalls between the systems.

 

For example, here is how you would clone the acme/foo.git repository using the git clone command and SSH. The installation and setup of Git does not setup an SSH server. You will need to have a working SSH server on the same system as your origin Git repository. If cloning a github.com repository, github.com supports SSH.

git clone ssh://john.doe@server1.example.com/acme/foo.git

 

Or clone a github.com repo using the gh repo clone command.

gh repo clone acme/foo

 

Be aware that when using the gh repo clone command this will clone the repo using HTTPS which can be seen with the git remote command.

~]$ git remote --verbose
origin  https://github.com/acme/foo.git (fetch)
origin  https://github.com/acme/foo.git (push)

 

Or clone using HTTPS with a username and password using the git clone command. Notice with https that the password can be included so that you are not prompted for the password. The installation and setup of Git does not setup an HTTPS server. You will need to have a working HTTPS server on the same system as your origin Git repository. If cloning a github.com repository, github.com supports HTTPS.

git clone https://<username>:<password>@github.com/acme/foo.git

 

Or clone using HTTPS with a username using the git clone command. In this example there will be an interactive prompt to provide your password.

git clone https://<username>@github.com/acme/foo.git

 

Or clone using HTTPS with a username and password using the git clone command.

git clone https://<username>:<password>@github.com/acme/foo.git

 

Or clone using HTTPS with a Personal Access Token (PAT) using the git clone command.

git clone https://<your personal access token>@github.com/acme/foo.git

 

Such as a GitHub Personal Access Token (PAT).

 

When cloning using SSH, if you are prompted for the SSH password or passphrase, you can use the ssh-keygen command to create an SSH public certificate and private key.

ssh-keygen -t ed25519 -N '' -C $(whoami)@$(hostname) -f $HOME/.ssh/id_ed25519

 

Optionally, you may want (or need to) use the ssh-agent and ssh-add commands to store your users private key for SSH connections.

~]# eval $(ssh-agent -s)
Agent pid 2023

~]$ ssh-add ~/.ssh/id_ed25519
Identity added: /home/john.doe/.ssh/id_ed25519 (john.doe@example.com)

 

Or create $HOME/.ssh/config with the following.

Host github.com
    IdentityFile /home/john.doe/.ssh/id_ed25519

 

Assuming your users public certificate has been appended to the authorized_keys file on the SSH server you should be able to clone the repository over SSH without having to provide your SSH private key.

git clone ssh://john.doe@server1.example.com/acme/foo.git

 

Or like this, to connect to a repository on https://github.com/.

AVOID TROUBLE

On August 13 2021, GitHub removed support for password authentication when attempting to clone a repository over HTTPS. Follow these steps to setup SSL key based authentication.

  1. Sign into https://github.com/.
  2. Select your profile > Settings.
  3. Select SSH and GPG Keys.
  4. Select New SSH Key.
  5. Paste the content of one of your public certificates in your $HOME/.ssh directory and select Add SSH Key.

When connecting to a repository on https://github.com/, you will always use username git. If some other username is used, the connection will fail.

git clone ssh://git@github.com/acme/foo.git

 

By default, the git clone command will clone the repository into your present working directory.  A target directory can be specified, like this. Be aware that the target directory must not already exist, as the target directory will be created as part of the cloning.

git clone https://github.com/foo.git /path/to/directory

 

If the repository is empty, something like this should be displayed.

warning: You appear to have cloned an empty repository.

 

And the git status command should return No commits yet.

~]$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

 

On the other hand, if the repository is not empty, something like this should be displayed.

Initialized empty Git repository in /path/to/directory/.git/
remote: Enumerating objects: 90, done.
remote: Counting objects: 100% (90/90), done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 90 (delta 29), reused 0 (delta 0)
Receiving objects: 100% (90/90), 131.06 KiB, done.
Resolving deltas: 100% (29/29), done.

 

Define your username and email using the git config command




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 543cf9 in the box below so that we can be sure you are a human.