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.
Learn more about Git
- What is Git and why it is used?
- How to install Git on Windows & Mac?
- How to use Git with Visual Studio Code?
- How to use Git and GitHub?
- What is the difference between Git and GitHub?
- How to connect local Git to a remote server?
- How to change Git remote origin URL?
- How to create a Git repository on GitHub?
- Git add all (stage all changes)
- How to undo the git add -A command?
- How to undo the last Git commit?
- How to change the git commit message?
- How to remove or unstage a file from Git commit?
- How to git push after rebase?
- Git switch branches: How to checkout the git branch?
- How to merge a git branch to master?
- Git branch: create, checkout, merge, list, command, delete & push
- How to clone a Git repository?
- How to git pull to override the local project?
- How to remove file from Git?
- When should you use git rebase?
- Git commands & explanation (downloadable cheatsheet included)
- How to create GitHub Pages?
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.