How To Clone A Git Repository - Devconnected
How To Clone A Git Repository - Devconnected
Software Engineering
Git is by far one of the most popular version control system available for developers.
Created in 2005 by Linus Torvalds, the creator of the Linux operating system, Git is built as a distributed envir
enabling multiple developers and teams to work together on the same codebase.
In order to start working with Git, you can either create your own Git repository or you can clone an existing G
repository.
We are also going to see different ways to clone a specific branch, to clone using a SSH key and to solve access
issues.
Table of Contents
1. Prerequisites
2. Clone a Git repository using git clone
3. Clone a Git repository into a specific folder
4. Git clone a specific branch
4.1. Git clone exclusively one branch
5. Clone a private Git repository
5.1. Clone using SSH
5.2. Specifying the SSH key to use
5.3. Clone using a password
5.3.1. Store your password using git credentials
6. Git Clone Authentication Failure
7. Conclusion
Prerequisites
In order to clone a git repository, you obviously need to have Git installed on your computer.
To check if Git is correctly installed on Windows or on Linux, you need to execute the following command
$ git --version
If Git is correctly installed, you are ready to start cloning your first Git repository.
For example, let’s say that you want to clone a public repository from Github, you are going to execute the follo
command
remote: Total 813 (delta 0), reused 0 (delta 0), pack-reused 813
As you can see, by default, Git is going to clone your Git repository into a folder named by the name of project.
However, you can choose to clone your Git repository into a different folder.
Clone a Git repository into a specific folder
In order to clone a git repository into a specific folder, execute the “git clone” command and specify the
destination folder at the end.
For example, given the Github project we fetched in the previous section, if we want to clone it into a folder na
“myproject” we would run
remote: Total 813 (delta 0), reused 0 (delta 0), pack-reused 813
Now, verify that you git project was correctly cloned to the destination folder.
$ ls -l
total 4
drwxrwxr-x 5 schkn schkn 4096 Nov 1 10:39 myproject
Awesome!
You successfully cloned a Git repository into a specific folder on your server.
In this case, you cloned the master branch from your Git remote repository.
You can check the current branch cloned by running the “git branch” command.
$ git branch
* master
However, in some cases, you may want to clone a specific branch in order to start working.
Your team may have chosen to let the “master” branch a bit behind and to have the most recent commits direc
“dev” branch for example.
For example, in order to clone the “dev” branch of your Github repository, you would run
remote: Total 813 (delta 0), reused 0 (delta 0), pack-reused 813
To verify that you correctly cloned the “dev” branch, make sure to run the “git branch” command.
$ git branch
* dev
Using the “-b” option, you are fetching all the branches but you are checking out the branch you chose.
It means that if you run “git branch” with the “-a” (for all) option, you are going to see that all your branches we
fetched.
Note : you have to execute this command into the Git repository you just cloned.
$ git branch -a
* dev
remotes/origin/dev
remotes/origin/master
remotes/origin/feature
Make sure that only the branch chosen was fetched on your local repository.
Note : you have to execute this command into the Git repository you just cloned.
$ git branch -a
* dev
remotes/origin/dev
This option works for Git versions greater than 1.17.10, so make sure that this is the case before issuing the co
$ git --version
In the previous sections, we saw the various ways to clone public repositories to your local server.
It means that you did not need to provide any username or password in order to clone the repositories.
However, in some cases, you may have private Git servers that only authorized team members can access.
In this section, we are going to see how you can authenticate to your Git server using both methods.
In order to clone from a private repository using SSH, your SSH keys need to be correctly set and config
your server.
Go into your personal “.ssh” directory and create a new SSH key named “repo_id_rsa” where repo stands for th
of the repository you are trying to clone.
SHA256:Lu0CV79iccFGzDLs4x6RXZbUOyimXRsIlNc0o30T+u4 [email protected]
+---[RSA 4096]----+
| o.+ +=o. |
| * =o=+.. |
| . X.+o.o. |
| * O +oo. |
| oSO + o.. |
| . .o= + .. |
| o..o+ . . |
| .o+ . . |
| o.. E |
+----[SHA256]-----+
Print down the public key content using the “cat” command.
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDKRsXF9r2DSgSHxYe6QkJE0Otekn5F9E5e+VQfFtzJAr/
vCuJbWWsPo6Fibbw54jYjEGjVhnMFOQl9nWA8KxubX6HUHtXxlw9VRVKob6OyO4Qt0F8nw== email@exam
om
On the server, head over to the “authorized_keys” file and add the content of your public key to the server.
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDKRsXF9r2DSgSHxYe6QkJE0Otekn5F9E5e+VQfFtzJAr/
vCuJbWWsPo6Fibbw54jYjEGjVhnMFOQl9nWA8KxubX6HUHtXxlw9VRVKob6OyO4Qt0F8nw== email@exam
om
Next, you should be able to clone the git repository using your newly created SSH keys.
To create a specify SSH key for your Git repositories, simply specify a name when prompted by the “ssh
utility.
In this case, if you try to clone the repository, you might not be able to do so because you need to tell SSH whic
use for the server.
To specify the SSH key to use, add the following content to your ~/.ssh/config file (you need to create it if it doe
already exist)
Host *
Hostname <server_ip>
User git
IdentityFile ~/.ssh/repo_id_rsa
If you were to use Github as a git server, it would give the following configuration.
Host *
Hostname github.com
User git
IdentityFile ~/.ssh/repo_id_rsa
To git clone using a password, simply provide the username for the git account, and you will be prompt
the password.
For example, if you want to clone your repository from Github using a password, simply type the following com
Luckily for you, you can set your password in order not to be prompted again.
To enable the credentials helper, simply run “git config” with the “credential.helper” option.
When cloning a new repository, you will still be asked to provide the password on the first time.
However, you won’t be asked to provide it again on push and pull operations.
Your credentials will be stored in the git-credentials file in your home directory.
$ cat ~/.git-credentials
https://<username>:<password>@<server>
Here are some hints on what you may check in order to solve this issue :
Make sure that you are writing the correct password when cloning a repository.
In the section dedicated to git clone with password, you may need to inspect the git-credentials file. If your pas
changed on the server, make sure to change it in your git-credentials file for the changes to be applied.
Make sure that the user account is correctly configured on the Git server.
In some cases, you may have an account on the Git server but you may not have enough rights to perform read
operations to the server.
Depending on the git server you are using (Gitlab, Gittea, Bitbucket), you need to check the account sections to
the user account is correctly configured.
When using authentication with SSH, you may need to check that your SSH client is actually using
key to connect to your server.
In order to check if OpenSSH is using your SSH key, use “ssh” with the “-vT” option.
When using SSH key based authentication, verify that you server contains the public key of your c
In order to check authorized keys on the server, make sure to inspect the authorized_keys in the .ssh directory
$ cat ~/.ssh/authorized_keys
ssh-rsa AAAABfzaC1yc2EAAAADAfgzrgegtexoq6FuKMPSs9cpeoCv+HUaL3fijO2otTw54451cfzfxpcd
YZp34qztJGC1AwNiU5yezfzi/D2afzfzzFls3wvwn+DNA [email protected]
Conclusion
In this tutorial, you learnt more about git clone and how it can be used in order to start working on different p
on your code base.