Bootstrap FreeKB - Git (Version Control) - Clone a repository using the git clone command
Git (Version Control) - Clone a repository using the git clone command

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.

 

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><absolute path to the repository to clone>

 

For example, here is how you would clone the foo.git repository using 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.

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

 

Or via HTTPS. 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.

git clone https://john.doe:itsasecret@server1.example.com/path/to/foo.git

 

AVOID TROUBLE

If there is a firewall between the local PC and the origin Git repository, SSH or HTTPS (or both) protocol will need to be allowed in the firewall.

 

By default, you will be prompted to enter your password when cloning the repository. If you want to avoid the password prompt, 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 2>&1 >/dev/null

 

And then 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)

 

Better yet, create /home/john.doe/.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 password.

git clone ssh://john.doe@server1.example.com/path/to/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 your /home/your_username/.ssh/id_rsa.pub public certificate 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/path/to/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://www.example.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 ddcb4c in the box below so that we can be sure you are a human.