Table of Contents
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.
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:
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:
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.pubcontent which we copied in the first step of this section.
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.
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.
Since we have already added our public key to our GitLab profile, we will use SSH URL:
deepak@ubuntu:~$ git clone firstname.lastname@example.org:golinuxcloud/helloworld.git Cloning into 'helloworld'... The authenticity of host 'gitlab.com (22.214.171.124)' 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,126.96.36.199' (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 "email@example.com"
To check the configuration, run:
deepak@ubuntu:~/helloworld$ git config --global --list user.name=Deepak Prasad firstname.lastname@example.org
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:
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.