Git clone repository

In the previous article we created our first git repository. Now let us perform git clone repository to have a local copy of the remote repo in a different server.

 

Step-1: Create Project Repo

This is an optional step. If you already have a repository then you can ignore this and continue with next step.

Advertisement

To demonstrate this article we will create a repository on GitLab. You can signup for a free account on gitlab for your test purpose. I will not go in detail about signing up or creating project on gitlab as it is quite straight forward. You can follow the on screen hints and you will be able to create your first project.

In my case I have created a private project “HelloWorld” as you can see below:

Git clone repository

NOTE:

In GitLab or GitHub, a repository is contained in a project so don’t get confuse between Project and Repository

Next we will clone this repo into our Ubuntu server running on my Windows 10 laptop as a VM.

 

Step-2: Create Password less authentication

The developer will have to access the remote respository multiple times to clone, pull, fetch, push operation during his/her course of development. So every time the developer wants to connect to the git server, they need to provide the password. To avoid this we will set up a password less authentication between git server and individual developer’s computer.

First step is generating private public key pair using ssh-keygen tool. Since we plan to use deepak user from ubuntu server to connect to our gitlab server, we will generate the key pair on the client node:
Git clone repository

 

Advertisement

Step-3: Add SSH Public Key to GitLab Server

Since we are using GitLab Server to create and manage our repository, we will copy our public key to the gitlab server’s profile.

  • Copy the content of your SSH public key which should be under ~/.ssh/id_rsa.pub (if you have followed the steps I showed above)
  • Login to your GitLab server profile.
  • Under your Profile icon in the right corner, select “Edit Profile
  • From the Left MENU tab, select SSH Keys an paste the id_rsa.pub content which we copied in the first step of this section.

Git clone repository

 

Next fill the Title and Expiry section. You may give any future date for the expiry and click on Add key which should save this key to your profile on GitLab.

Git clone repository

 

Step-4: Perform git clone repository

When you clone a repository, the files from the remote repository are downloaded to your computer, and a connection is created. This connection requires you to add credentials. You can either use SSH or HTTPS. SSH is recommended.

To perform a clone of the repository, under your project repo you will find an option to Clone as shown in the below image. Under clone you will have an option to clone using SSH or HTTPS.

Git clone repository

 

Since we have already added our public key to our GitLab profile, we will use SSH URL:

deepak@ubuntu:~$ git clone git@gitlab.com:golinuxcloud/helloworld.git
Cloning into 'helloworld'...
The authenticity of host 'gitlab.com (172.65.251.78)' can't be established.
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.com,172.65.251.78' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), done.

Next verify the repo contents. As of now we have not added any files to our repo so we can only find .git directory here.

deepak@ubuntu:~$ ls -al helloworld/
total 12
drwxrwxr-x  3 deepak deepak 4096 Jul 15 08:18 .
drwxr-xr-x 19 deepak deepak 4096 Jul 15 08:18 ..
drwxrwxr-x  8 deepak deepak 4096 Jul 15 08:18 .git

 

Difference between download and clone

To create a copy of a remote repository’s files on your computer, you can either download or clone the repository. If you download it, you cannot sync the repository with the remote repository on Git Server.

Cloning a repository is the same as downloading, except it preserves the Git connection with the remote repository. You can then modify the files locally and upload the changes to the remote repository on GitLab

 

Step-5: Configure git

To start using Git from your computer, you must enter your credentials to identify yourself as the author of your work. The username and email address should match the ones you use in GitLab.

In your shell, add your user name:

deepak@ubuntu:~/helloworld$ git config --global user.name "Deepak Prasad"

Add your email address:

deepak@ubuntu:~/helloworld$ git config --global user.email "admin@golinuxcloud.com"

To check the configuration, run:

deepak@ubuntu:~/helloworld$ git config --global --list
user.name=Deepak Prasad
user.email=admin@golinuxcloud.com

 

Step-6: Commit and Push changes to Git Server

Next we will create a dummy file on our HelloWorld repository.

deepak@ubuntu:~/helloworld$ touch file1

Add this file to git repo:

deepak@ubuntu:~/helloworld$ git add file1

Commit the changes with some message:

deepak@ubuntu:~/helloworld$ git commit -m "Some File" file1
[master add2ebf] Some File
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1

Check your repository branch. Since we have not created any branch, so we will be using master branch by default:

deepak@ubuntu:~/helloworld$ git branch
* master

So we will push our changes to the master branch itself:

deepak@ubuntu:~/helloworld$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (3/3), 244 bytes | 24.00 KiB/s, done.
Total 3 (delta 0), reused 1 (delta 0), pack-reused 0
To gitlab.com:golinuxcloud/helloworld.git
   b6fcb01..add2ebf  master -> master

Now you can verify on Git Server, this newly created file should be present:

Git clone repository

 

Summary

In this tutorial we learned how to perform git clone repository from a remote git server to local workstation. There is a difference between cloning and downloading a repo so you should know your requirement before you perform this action. When there are multiple developers working on single project then individual developers should clone the repo and work on their local repo. Once the changes are reviewed, then it can be committed and pushed to remote repository.

 

Further Readings

git clone repository

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment