Git is a popular version control system used by many businesses around the world. However, it requires you to have a copy of the git repository before you can begin working on it. You need to clone a git repository to your local desktop or laptop, before you can start making any changes to it. In this article, we will look at how to clone a git repository in Ubuntu. You can use these steps to clone a repository in github, bitbucket, gitlab and other popular git-based developer platforms.
What is git clone?
Git clone is basically a command to point to an existing repository and make a copy of it, at another location. In this case, the git command will create a new directory, set it up for use with git and copy files into it. Unless you clone a git repo, you will not be able to contribute changes to it.
How To Clone Git Repository in Ubuntu
There are different ways to clone a git repository, depending on your requirement. We will look at each case separately.
Clone a remote repository
Let us say you want to clone a remote repository from Github, Bitbucket, or any other cloud platform to your local machine.
Open terminal and navigate to the location (e.g /home/ubuntu) where you want the repo to be copied.
#cd /home/ubuntu/
Also read : How to Install Git in Ubuntu
Every remote git repo will have a URL. Log into your development platform such as Github, and note its URL. It will be of the format.
https://git-website.com/username/repository-name
For example, your git repo (e.g demo) will have the following URLs, depending on the platform.
Github
https://github.com/username/demo.git
Bitbucket
https://bitbucket.com/username/demo.git
Sometimes, the URL can also be of the form
ssh://username@example.com/path/to/my-project.git
Also read : How to Install Virtualenv in Ubuntu
Note the URL for your repository, and use it in the git clone command as shown below.
# sudo git clone https://github.com/username/repository-name
Replace username with your Github username, and repository-name with your repository’s name. For example,
# sudo git clone https://github.com/test_user/demo.git
You will be asked for your password for authentication, after which Git will automatically download the copy of your repository to your present working directory.
Also read : How to Upgrade Python in Ubuntu
Cloning to a specific folder
Here’s the command to clone your repository to a specific folder.
# sudo git clone <repo> <directory>
For example, let us say you want to clone your repository to /home/developer folder
# sudo git clone https://github.com/test_user/demo.git
/home/developer
Shallow clone a repository
If you need to clone a big repository with large commit history, it can be very time consuming. In such cases, you can do a shallow clone where you can specify the last n commits you want to be cloned. It will be much faster and take up very little space on your system.
Here is the syntax for shallow clone, where n is the number of most recent commits you want to clone.
# sudo git clone -depth=n <repo>
Also read : VPS vs Shared Hosting – In-Depth Comparison
Here is the command to clone the last 1 commit of your repository.
# sudo git clone -depth=1 https://github.com/test_user/demo.git
Similarly, here is the command to clone the last 10 commits of your repository.
# sudo git clone -depth=10 https://github.com/test_user/demo.git
Clone a Git Branch
If you want to only clone a specific branch (e.g working) and not the entire repository, use the -branch option in git clone.
# git clone -branch working https://github.com/test_user/demo.git
That’s it. As you can see, it is very easy to clone a git repository in Ubuntu.
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.