How to use the git clone command with GitHub by example
A developer only needs two steps to get started with Git. First, create a new local repository with the git init command. Second, issue the git clone command to locally copy the contents of a remotely hosted Git repository. In this git clone command tutorial, we provide an example of how to do the latter.
Before you start, set up a local network connection and a local Git installation.
Here's how to clone your remote-hosted Git repository with git clone in three easy steps.
Step 1: Obtain the Git repository URL
All remote repositories, such as GitHub, GitLab or even a custom one that an organization hosts on the local network, have a URL associated with the repository. This tutorial uses GitHub, and you can find the repository URL when you click on the Clone or download button on the repository's GitHub page.
Git url: https://github.com/cameronmcnz/rock-paper-scissors.git
Copy this URL to the clipboard because you'll need it in a future step.
Step 2: Run the git clone command
With the Git URL copied, open a terminal or command window in the folder where you will maintain the remote repository and issue the git clone command:
##The git clone command should all be on one line
gitinit@example:~$ git clone
https://github.com/cameronmcnz/rock-paper-scissors.git
As the git clone command executes, the local system will download the contents of the remote repository, including all the remote references to Git branches and the contents of the current workspace.
Step 3: Validate the cloned repository
After the git clone command completes, validate the downloaded repository. Navigate into the subfolder that contains the cloned data and check if the working directory has the files listed on the server. In this example, a valid cloned repository means seeing files such as game.html and pom.xml in the listing.
gitinit@example:~$ cd rock*
gitinit@example:~/rock-paper-scissors$ ls
game.html pom.xml Jenkinsfile
scripted-pipeline src
There's another way to validate a cloned repository. Issue Git commands such as git log or reflog. The version control system will display a history of Git commits going back to the date the repository was created and should show the new history that stems from newly cloned repositories on the local machine.
Clone a GitHub repository in 3 steps
In review, the steps to clone a remote Git repository are:
- Obtain the Git URL of the remote repository.
- Issue the git clone command.
- Validate the locally cloned git repository.
There are several other ways to clone a remote Git repository. Most Git GUI tools, such as GitHub Desktop, provide built-in features to do so. But the simplest, and most universal way to clone a remote Git repository is to use the terminal window and simply issue the git clone command. As any Git expert will tell you, there's value in familiarity with the low-level commands that make this popular distributed version control tool work.