In this post, I will show how you can clone a Git repository using the command line (terminal). Also, I will give you a real repository to clone on your computer (for practice).
You may have created brand-new repositories in the past. But why do you need to clone a Git repository and why this is helpful?
To answer this question, I want to give you an example first. Let’s imagine, you & I are coworkers, I have been working on a website and I want you to help me. So the website already exists. In this case, you should not create a brand-new repo. Instead, you need to take a copy or clone the existing repo to your computer and start working on it. And this is where cloning a Git repository comes into play.
How to clone a Git repository in your computer hard drive?
Prerequisite: Make sure you have Git installed on your machine and have access to the repository URL before you proceed.
To clone a Git repository, type the following command:
git clone repository_url_goes_here
Don’t forget to replace the “repository_url_goes_here” with the actual Git repository URL that ends with “.git”
This repository could be from GitHub, Bitbucket, or anything else. The process is ultimately the same for all Git hosting servers.
Make your hands dirty (clone a real Git repository for practice)
In this section, I will walk you through the step-by-step process of cloning a real Git repository for practice. So you’ll know how to do it in the real world and how easy it is!
I have created this simple static portfolio website and here is the GitHub repository for this portfolio site. The URL for this repository is https://github.com/shihabiiuc/portfolio.git
Step 1: Create a folder on your computer & navigate to the folder in your command line
First, create a folder named “test-project” on your computer. Open your terminal or Git Bash or command line, and navigate to the “test-project” folder.
Step 2: Clone the Git repository
Now type the following exact command in your command line & hit enter
git clone https://github.com/shihabiiuc/portfolio.git
After you hit enter, it will start cloning my repository in your “test-project” folder.
This will take a minute or two to complete. Once it finishes, you’ll find a new folder named “portfolio” in your “test-project” folder.
Well done! You just cloned a Git repository from GitHub to your computer hard drive.
Open the portfolio folder, and drag & drop the “index.html” file into your web browser. You’ll see the same portfolio website as the demo.
How to store this cloned repository in your own GitHub account?
You cloned a Git repository from my GitHub account. Now if you make edits and want to use this template as your portfolio, you may want to store this repository in your own GitHub account instead of mine.
Not to mention, it could be GitHub or Bitbucket, or something else. This is up to you which Git hosting server you choose.
Anyways, when you clone a git repository, it also clones the remote URL. To see this in action, navigate to the “portfolio” folder in your command line.
Currently, your command line may be pointing to the “test-project” folder. Type
cd portfolio to navigate to the “portfolio” folder. (cd refers to change directory)
Once you’re in the “portfolio” folder, type the following command to see the current remote URL:
git remote -v
To change this remote URL, type the following command:
git remote set-url origin your_own_repo_url_goes_here
The above command will replace the existing remote URL with yours. For more details, see how to change the remote Git URL.
Before changing the remote URL, you have to create your repository first. If you want to use GitHub to create the repository, I have a detailed step-by-step walkthrough about it and it may take you 2/3 minutes to understand. See the post here.
Cloning a remote Git repository in the command line is easier than downloading a repo in a Zip format. I showed you how to clone a remote Git repository. Also, I helped you to clone a real & live repository that contains a static portfolio website. Hopefully, this will help you to make your hands dirty.
I tried to explain every bit of information with commands, screenshots, providing project files, etc. Therefore if you still have any questions about cloning git repositories, let me know. For your further reading, I included a couple of other related posts below. These will give you a rock-solid understanding Git.
|What is Git and why it is used?|
I understand you already know what is Git. But I provided additional information such as why people use it in the first place, its main features, why people love it so much, etc. It’s worth reading.
|How to connect local Git to a remote server?|
|How to use Git and GitHub?|
|What is the difference between Git and GitHub?|
|Why is Git useful for an individual developer?|
Some of you may think that Git is only applicable or helpful for teams. But this is not the case. I mentioned this article to inspire you to learn Git in-depth.
|Git branch: create, checkout, merge, list, command, delete & push|
You’ll learn mostly used Git commands that are related to feature branches.